How to Copy a Folder in SharePoint Online using PowerShell?

Requirement: Copy a Folder in SharePoint Online using PowerShell.

How to Copy Folder in SharePoint Online?

Copying a folder in SharePoint Online can be a useful way to quickly duplicate content and save time when creating new sites or libraries. 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 article, we will show you how to use PowerShell to copy folders between SharePoint sites and within site.

Copying a folder in SharePoint Online modern experience is simple:

  1. Navigate to your SharePoint Online Document Library >> Select the Folder to copy.
  2. From the Toolbar, click on the “Copy to” button.
    sharepoint online copy folder powershell
  3. 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
  4. 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 the SharePoint Online classic experience. While the above method to copy folders in SharePoint Online is simple, using PowerShell is the most efficient and flexible method in many cases.

Tips: You can also use “File Explorer” view to copy a folder in SharePoint Online!

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 permission to both sites 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://Crescent.sharepoint.com/sites/Marketing"
$SourceFolderURL="https://Crescent.sharepoint.com/sites/Marketing/Shared Documents/2017"
$TargetFolderURL="https://Crescent.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 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://Crescent.sharepoint.com/sites/marketing"
$SourceFolderURL= "Shared Documents/2017"
$TargetFolderURL = "Shared Documents/2018"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

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

This PowerShell script copies the given folder with 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://Crescent.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 

The “Copy-PnPFolder” cmdlet also does the same thing. 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 - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

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

  • Thank you for the post. I have an issue. Powershell says:
    Url Message ItemId
    — ——- ——
    Files/Documents The source folder ‘Files/Documents’ is already part of another copy/move operation 40414
    How can I fix it.
    Thanks

    Reply
    • Hi Andrey, did you find a solution for this?

      Reply
  • 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

Your email address will not be published. Required fields are marked *