Answers for "powershell sharepoint copy file to other site collection"

0

powershell sharepoint copy file to other site collection

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
Posted by: Guest on March-22-2020

Code answers related to "powershell sharepoint copy file to other site collection"

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language