SharePoint Online: Copy a Document Library using PowerShell

Requirement: PowerShell to Copy a Document Library in SharePoint Online

How to copy a Document Library in SharePoint Online?

Well, there are no direct ways to copy a document library in SharePoint Online. However, You can use this workaround:

  1. Save the source document library as a template – Go to the source library >> Click on Settings gear >> Choose Library Settings >> Click on “Save as Template”. More info: How to Save List as Template in SharePoint Online?
  2. Create a new document library from the list template – Login to your SharePoint Online site >> Click on the Settings gear icon and click “Add an app”. You can use the pagination at the bottom of the page to find your custom list template or use the “Find an App” search box and pick your list template. More info: SharePoint Online: Create List from Custom Template using PowerShell
  3. Copy all items from the source document library to the new document library – Once you created a new instance of the document library from the existing list template, you can copy all files and folders from the source library to the destination library by selecting all items in the source library and choose “Copy to” button in the toolbar and then specify the document library created in step 2.
    sharepoint online powershell copy document library

Make sure you have a custom script enabled for the site collection before proceeding with this method! How to Enable Custom Script in SharePoint Online?

SharePoint Online: PowerShell to Copy a Document Library

Let’s automate the above steps to copy a document library in SharePoint Online using PowerShell:

$SiteURL = ""
$SourceLibraryName = "Documents"
$DestinationLibraryName = "DocumentsV2"

Try {
    #Connect to the Site
    Connect-PnPOnline -URL $SourceSiteURL -UseWebLogin

    #Get the Source library
    $SourceLibrary =  Get-PnPList -Identity $SourceLibraryName  -Includes RootFolder

    #Step 1: Save the  Source Library as a template
    $SourceLibrary.SaveAsTemplate($SourceLibrary.Title, $SourceLibrary.Title, [string]::Empty, $False);

    #Get the Library Template created
    $Ctx = Get-PnPContext
    $Web = Get-PnPWeb
    $RootWeb = $Ctx.Site.RootWeb
    $ListTemplates = $Ctx.Site.GetCustomListTemplates($RootWeb)
    $ListTemplate = $ListTemplates | Where {$_.Name -eq $SourceLibraryName}
    #Step 2:  Create the destination library from the source library template
    If(!(Get-PnPList -Identity $DestinationLibraryName))
        #Create the destination library
        $ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListCreation.Title = $DestinationLibraryName
        $ListCreation.ListTemplate = $ListTemplate
        $DestinationList = $Web.Lists.Add($ListCreation)
        Write-host "New Library '$DestinationLibraryName' created successfully!" -f Green
        Write-host "Library '$DestinationLibraryName' already exists!" -f Yellow
    #Remove the List Template
    Remove-PnPFile -SiteRelativeUrl ("/_catalogs/lt/"+$ListTemplate.InternalName) -Recycle -Force

    #Step 3: Copy content from Source to the destination library
    $DestinationLibrary = Get-PnPList $DestinationLibraryName -Includes RootFolder
    $SourceLibraryURL = $SourceLibrary.RootFolder.ServerRelativeUrl
    $DestinationLibraryURL = $DestinationLibrary.RootFolder.ServerRelativeUrl

    #Copy All Content from Source Library to the Destination Library
    Copy-PnPFile -SourceUrl $SourceLibraryURL -TargetUrl $DestinationLibraryURL -SkipSourceFolderName -Force -OverwriteIfAlreadyExists
    Write-host "`tContent Copied from $SourceLibraryURL to  $DestinationLibraryURL Successfully!" -f Green
Catch {
    write-host -f Red "Error:" $_.Exception.Message

This PowerShell method retains all metadata except created/modified timestamps and created by, modified by field values.

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

Leave a Reply