kwizcom banner advertisement

How to Copy a Folder in SharePoint Online using PowerShell?

Requirement: Copy Folder in SharePoint Online using PowerShell

How to a Copy Folder in SharePoint Online?
Copying a folder in SharePoint Online modern experience is quite simple.
  • Navigate to your SharePoint Online Document Library >> Select the Folder to Copy
  • From the Toolbar, Click on "Copy to" button
    sharepoint online copy folder powershell
  • Select the destination location you want to Copy the folder. You can either choose the same document library/site or choose a different site collection or subsite. Use "Browse Sites" to see a full list of sites that you can copy to.
    sharepoint online copy folder
  • Click on "New Folder" and provide a name to the copy and then Click on "Copy here" button to start copying the folder.
    copy folder in sharepoint online
This Copies the folder with all its files & sub-folders to the selected destination. Please note, the "Copy To" is not available in SharePoint Online classic experience.

SharePoint Online: Copy Folder using PowerShell
Lets copy a folder in SharePoint Online using PowerShell
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Function to Copy a Folder
Function Copy-SPOFolder([String]$SiteURL, [String]$SourceFolderURL, [String]$TargetFolderURL)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #Copy the Folder
        $MoveCopyOpt = New-Object Microsoft.SharePoint.Client.MoveCopyOptions
        [Microsoft.SharePoint.Client.MoveCopyUtil]::CopyFolder($Ctx, $SourceFolderURL, $TargetFolderURL, $MoveCopyOpt)
        $Ctx.ExecuteQuery()
 
        Write-host -f Green "Folder Copied Successfully!"
    }
    Catch {
    write-host -f Red "Error Copying the Folder!" $_.Exception.Message
    }
}
 
#Set Config Parameters
$SiteURL="https://crescenttech.sharepoint.com/sites/Marketing"
$SourceFolderURL="https://crescenttech.sharepoint.com/sites/Marketing/Shared Documents/2017"
$TargetFolderURL="https://crescenttech.sharepoint.com/sites/Marketing/Shared Documents/2019"
 
#Get Credentials to connect
$Cred= Get-Credential
 
#Call the function to Copy the Folder
Copy-SPOFolder $SiteURL $SourceFolderURL $TargetFolderURL

Here is my another article to move folder in SharePoint Online: SharePoint Online PowerShell to Move Folder

Copy a Folder in SharePoint Online using PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$SourceFolderURL= "Shared Documents/2017"
$TargetFolderURL = "Shared Documents/2018"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Copy Folder to same library
Copy-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Force

This PowerShell script copies the given folder with all its sub-folders and files to destination. Similarly, you can use this method to copy a folder to different site collection even!
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$SourceFolderURL= "Shared Documents/2017"
$TargetFolderURL = "/Sites/Sales/Shared Documents/2017"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Copy Folder to different site collection
Copy-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Force 
How to Copy a Folder in SharePoint Online using PowerShell? How to Copy a Folder in SharePoint Online using PowerShell? Reviewed by Salaudeen Rajack on April 18, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.