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 the “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 the “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

Let’s 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

We can also use this script to copy a folder to another library SharePoint Online. 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 the destination. Similarly, you can use this method to copy a folder to a 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 

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

3 thoughts on “How to Copy a Folder in SharePoint Online using PowerShell?

  • August 14, 2019 at 6:01 AM

    I have been banging my head against a wall with this PnP script. I can move a single file without issue, but not a folder. I keep getting the error:
    Copy-PnPFile : The directory is not a subdirectory of the root directory. (Exception from
    HRESULT: 0x80070090)

    Reply
  • July 5, 2019 at 12:12 PM

    Thanks for sharing this, You just saved me a whole lot of time and stress

    Reply
  • July 5, 2019 at 12:12 PM

    Thanks for sharing this, it just saved me a whole lot of stress

    Reply

Leave a Reply