PowerShell Script to Copy Documents Between Sites in SharePoint 2007 or SharePoint 2010

PowerShell Script to Copy Documents Between Sites in SharePoint 2007 or SharePoint 2010

I have observed that now a days a very common and cumbersome task for the administrators is to copy documents between sites. I have observed that workflows are used to do the same. To provide more flexibility I have written the following script. This script is executed in two modes in the first mode it copies all the files from source folder to the destination folder. These folders can be the document library or the folders within them. When you will be asked for the folders URL you need to provide that in the following format.
http://site/library or http://site/library/Foldername . In the second way you can choose to copy only one file among all files in the source folder to copy to destination folder. This works in MOSS 2007 as well as in SPS 2010.

param([switch]$help)

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

function GetHelp() {

$HelpText = @"

DESCRIPTION:
This script can be executed in two modes in the first mode it will copy all files from the source folder to the destination folder, in the second mode it will copy the
specific file from the source folder to destination folder. This folder can be a document library or a folder inside the document library.
This will also check in the file so that it will be visible to authorized users. If the file already exists in the destination location this tool will overwrite the
file.
"@

$HelpText

}

function RahulCopyDocumentsInSharepoint() {

 write-host "Hello, This tool will copy the the files between the folders specified. It will overwrite the file if it exists at the destination location"
        write-host "Please enter the URL of the source folder"
        $sourceURL = read-host
        write-host "Please enter the URL of the destination folder"
        $destinationURL = read-host
        write-host "do you want to copy a specific file?if yes then type y or else type anything else"
        $fileDecision = read-host
        $cFile = [String]::Empty
        if($fileDecision -eq "y")
                {
                    write-host "Please enter the filename with extension of the file"
                    $cFile = read-host
                }
        $sourceSite = New-Object Microsoft.SharePoint.SPSite($sourceURL)
        $sourceWeb = $sourceSite.OpenWeb()
        $destinationSite = New-Object Microsoft.SharePoint.SPSite($destinationURL)
        $destinationWeb = $destinationSite.OpenWeb()
        $sList = [Microsoft.Sharepoint.SPFolder]$sourceWeb.GetFolder($sourceURL)
        $dList = [Microsoft.Sharepoint.SPFolder]$destinationWeb.GetFolder($destinationURL)
        $dLibrary = $destinationWeb.Lists[$dList.ContainingDocumentLibrary]
        $files = $sList.Files
        if ($fileDecision -ne "y")
        {
        foreach ($ctFile in $files)
        {
        $sbytes = $ctFile.OpenBinary()
        $dListFiles = $dList.Files
        foreach ($dListFile in $dListFiles)
        {
        if($dListFile.Name.Equals($ctFile.Name))
         {
         $dListFile.CheckOut()
         }
         }
        $dFileName = $dList.Files.Add($ctFile.Name, $sbytes, $true)
        if ($dFileName.CheckOutStatus -ne [Microsoft.Sharepoint.SPFile]::SPCheckOutStatus::None)
           {
           $dFileName.CheckIn("Checking in")
            }
            }
            }
            else
            {
            $desFile = $sList.Files[$sourceURL +"/" + $cFile]
            $sbytes = $desFile.OpenBinary()
             $dListFiles = $dList.Files
            foreach ($dListFile in $dListFiles)
             {
            if($dListFile.Name.Equals($desFile.Name))
               {
          $dListFile.CheckOut()
              }
                                        }
            $dFileName = $dList.Files.Add($desFile.Name, $sbytes, $true)
            if ($dFileName.CheckOutStatus -ne [Microsoft.Sharepoint.SPFile]::SPCheckOutStatus::None)
            {
          $dFileName.CheckIn("Checking in")
             }
            }
                                $destinationWeb.Update()
                                write-host "The operation completed successfully"

$sourceSite.Dispose()
$sourceWeb.Dispose()
$destinationSite.Dispose()
$destinationWeb.Dispose()
       
}


if($help) { GetHelp; Continue }
else { RahulCopyDocumentsInSharepoint }


I hope this will help you out.

Thanks,
Rahul Rashu
Leave a Comment
  • Please add 6 and 2 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Comments
  • Richard Mueller edited Revision 2. Comment: Removed (en-US) from title

  • Ed Price - MSFT edited Original. Comment: Title casing and minor edits

  • Craig Lussier edited Revision 1. Comment: added en-US to tags and title

Page 1 of 1 (3 items)
Wikis - Comment List
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Richard Mueller edited Revision 2. Comment: Removed (en-US) from title

  • Ed Price - MSFT edited Original. Comment: Title casing and minor edits

  • Craig Lussier edited Revision 1. Comment: added en-US to tags and title

Page 1 of 1 (3 items)