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?

If you need to copy a folder in SharePoint Online, PowerShell can make the process much easier than using the standard SharePoint user interface. In this blog post, we’ll show you how to use PowerShell to copy folders between SharePoint sites and within a site.

Copying a folder in SharePoint Online modern experience is 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

If you need to copy a folder in SharePoint Online, PowerShell provides an easy way to do it. Let’s copy a folder in SharePoint Online using PowerShell (Keep in mind that you’ll need permissions to both sites in order to copy the folder):

#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 a folder in SharePoint Online: SharePoint Online PowerShell to Move Folder

Copy a Folder in SharePoint Online using PnP PowerShell

Let’s copy a folder from one folder to another 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 

If you want to copy a folder to SharePoint Online from your local machine, use: How to Upload a Folder to SharePoint Online using PowerShell?

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!

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

  • I’m finding that this is copying the entire document library rather than only the specified folder.

    Reply
    • Confirmed its copying only given folder and its contents! Make sure the URLs are pointing to the right folders, and you have the latest Pnp.PowerShell module installed.

      Reply
  • 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
  • Thanks for sharing this, You just saved me a whole lot of time and stress

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

    Reply

Leave a Reply