Search code examples
powershellsharepointsharepoint-2013

SharePoint 2013 Powershell - Copy File From One Site Collection To Another


Please can someone assist in helping with the above subject?

I would like to copy one file from a specific folder in a sharepoint site collection to another library (of the same name) in a different sharepoint site collection (but still within the same Web Application). I have very little Powershell experience and have tried a number of Google searches but cannot seem to find anything that works.

Below is an example of what i have tried to do (lots of Write-Host to try and figure out what is going on) with the error message at the bottom.

Add-PSSnapIn "Microsoft.SharePoint.PowerShell"

## 
#Set Static Variables 
## 

$SourceWebURL = "http://WebAppURL/sites/Area/Master" 
$SourceLibraryTitle = "Web" 
$DestinationWebURL = "http://WebAppURL/sites/OtherSiteName" 
$DestinationLibraryTitle = "Web"
$FileName = "Resources.aspx"

## 
#Begin Script 
## 

$sWeb = Get-SPWeb $SourceWebURL 
$sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle} 
$dWeb = Get-SPWeb $DestinationWebURL 
$dList = $dWeb.Lists | ? {$_.title -like $DestinationLibraryTitle} 

$DestFolder = $dList.Files

$RootFolder = $sList.RootFolder
Write-Host " line 25 -- " $RootFolder  
$collfiles1 = $RootFolder.Files
Write-Host " line 27 -- "$collfiles1
Write-Host " line 28 -- "$DestFolder

Write-Host " line 30 -- "$str = $DestinationWebURL"/"$DestinationLibraryTitle"/"$FileName 
Write-Host  " line 31 -- "$collfiles1.Count
for($i = 0 ; $i -lt $collfiles1.Count ; $i++)
{
    Write-Host " line 34 -- "$collfiles1[$i].Name
    ##Write-Host $FileName
    if($collfiles1[$i].Name -eq $FileName)
    {
    ##  $str = $DestinationWebURL.Url + $DestinationLibraryTitle + "/" + $FileName
        $str = $DestinationWebURL+"/" +$DestinationLibraryTitle+"/"
        Write-Host " line 40 -- "$str
        Write-Host " line 41 -- "$collfiles1[$i]
        $FiletoCopy = $collfiles1[$i].Name
        Write-Host " line 43 -- " $FiletoCopy
        $FiletoCopy.CopyTo($str,$true)
    }
}
Write-Host "Script Completed"

The below example gives the error

Cannot find an overload for "CopyTo" and the argument count: "2".
At line:44 char:3
+         $FiletoCopy.CopyTo($str,$true)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

If someone could point me in the right direction that would be very helpful.

Thanks in advance, Ian.


Solution

  • The following PowerShell for your reference, copy a file from one library in site collection to another library in another site collection with fields.

    Add-PSSnapIn "Microsoft.SharePoint.PowerShell"
    
    ## 
    #Set Static Variables 
    ## 
    
    $SourceWebURL = "http://WebAppURL/sites/Area/Master" 
    $SourceLibraryTitle = "Web" 
    $DestinationWebURL = "http://WebAppURL/sites/OtherSiteName" 
    $DestinationLibraryTitle = "Web"
    $FileName = "Resources.aspx"
    
    ## 
    #Begin Script 
    ## 
    
    $sWeb = Get-SPWeb $SourceWebURL 
    #$sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle} 
    $dWeb = Get-SPWeb $DestinationWebURL 
    #$dList = $dWeb.Lists | ? {$_.title -like $DestinationLibraryTitle} 
    
    $SourceFile=$sWeb.GetFile($SourceWebURL+"/"+$SourceLibraryTitle+"/"+$FileName)
    $TargetFolder = $dWeb.GetFolder($DestinationLibraryTitle)
    #Copy File from the Source
    $NewFile = $TargetFolder.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(),$True)
    
    #Copy Meta-Data from Source
    Foreach($Field in $SourceFile.Item.Fields)
    {
        If(!$Field.ReadOnlyField)
        {
            if($NewFile.Item.Fields.ContainsField($Field.InternalName))
            {
                $NewFile.Item[$Field.InternalName] = $SourceFile.Item[$Field.InternalName]
            }
        }
    }
    #Update
    $NewFile.Item.UpdateOverwriteVersion()
    
    Write-host "Copied File:"$SourceFile.Name
    

    Reference: Copy Files Between Document Libraries in SharePoint using PowerShell