1 min readCopy files between SharePoint Online Sites using PowerShell

1 min readCopy files between SharePoint Online Sites using PowerShell

In this blog, we shall check how to copy the document files between the
SharePoint Online sites using PowerShell. This script requires
SharePoint Online PnP module, install it from
here.

Param(
[Parameter(ParameterSetName = "Inputparameter",Position=1,Mandatory=$True)]
[String]$sharepointUrl,
[Parameter(ParameterSetName = "Inputparameter",Position=2,Mandatory=$True)]
[String]$targeturl,
[Parameter(ParameterSetName = "Inputparameter",Position=3,Mandatory=$True)]
[String]$Sourceurl,
[Parameter(ParameterSetName = "Inputparameter",Position=4,Mandatory=$True)]
[String]$SourceDocumentLib
)
function Copy-sharepoint
{
Param(
[Parameter(Mandatory=$True)]
[String]$sharepointUrl,
[Parameter(Mandatory=$True)]
[String]$targeturl,
[Parameter(Mandatory=$True)]
[String]$Sourceurl,
[Parameter(Mandatory=$True)]
[String]$SourceDocumentLib,
[Parameter(Mandatory=$True)]
[String]$UserName,
[Parameter(Mandatory=$True)]
[String]$Password,
[Parameter(Mandatory=$True)]
[System.Management.Automation.PSCredential]$cred
)
#create secure password
$sPassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
Connect-PnPOnline -Url $Sourceurl -Credentials $cred
$Files= Find-PnPFile -List $SourceDocumentLib -Match *
Disconnect-PnPOnline
$Loginsource =$false
$webClient = New-Object System.Net.WebClient 
$webClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $sPassword)
$webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$webclient.Proxy = $null
foreach($file in  $Files)
{
    $pageUrl = $sharepointUrl+$file.ServerRelativeUrl
    $UniqueFileName = $file.Name
    $ByteArray=$webClient.DownloadData($pageUrl)
    $tfolderWithname= ($pageUrl -split $Sourceurl)[1]
    $tfolderwiths =($tfolderWithname -split $UniqueFileName)[0]
    $tfolderrmstr =$tfolderwiths.TrimStart("/")
    $tfolder =$tfolderrmstr.TrimEnd("/")
    $fstream = [System.IO.MemoryStream]($ByteArray)
    If($tfolder -notlike "*Forms")
    {
        If($Loginsource -eq $false )
        {
            Connect-PnPOnline -Url $targeturl -Credentials $cred
            $Loginsource =$true
            Add-PnPFile -FileName $UniqueFileName -Folder $tfolder -Stream $fstream
        }
        else
        {
            Add-PnPFile -FileName $UniqueFileName -Folder $tfolder -Stream $fstream
        }
    }
}
}
$cred=Get-Credential
$UName=$cred.UserName.ToString()
$Pass =$cred.GetNetworkCredential().Password
Copy-sharepoint -UserName $UName -Password $Pass -cred $cred -sharepointUrl $sharepointUrl -targeturl $targeturl -Sourceurl $Sourceurl -SourceDocumentLib $SourceDocumentLib
        

Migrate Everything to Microsoft 365

Exchange Online SharePoint Online OneDrive For Business Microsoft Teams Microsoft Planner Viva Engage (Yammer) Microsoft Bookings Microsoft Forms Power Automate Microsoft Power BI Exchange Online SharePoint Online OneDrive For Business Microsoft Teams Microsoft Planner Viva Engage (Yammer) Microsoft Bookings Microsoft Forms Power Automate Microsoft Power BI
  • No Data Loss
  • Zero Downtime
  • ISO-Certified Protection

Start your free 15-days trial today !


4.5 out of 5