SharePoint 2007 or 2010: Using PowerShell to Copy Documents Between Sites
Nowadays a very common and cumbersome task for the administrators is to copy documents between sites.
To provide more flexibility see 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 SharePoint 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 thespecific 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 thefile."@$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 }
Other Languages
This article is also available in the following languages: