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:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$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);
    Invoke-PnPQuery 

    #Get the Library Template created
    $Ctx = Get-PnPContext
    $Web = Get-PnPWeb
    $RootWeb = $Ctx.Site.RootWeb
    $ListTemplates = $Ctx.Site.GetCustomListTemplates($RootWeb)
    $Ctx.Load($RootWeb)
    $Ctx.Load($ListTemplates)
    Invoke-PnPQuery
    $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)
        Invoke-PnPQuery
        Write-host "New Library '$DestinationLibraryName' created successfully!" -f Green
    }
    Else
    {
        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

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!

One thought on “SharePoint Online: Copy a Document Library using PowerShell

  • September 16, 2021 at 12:46 PM

    Hi,

    Thx for your post, really helpfull !!!

    But, it seems that the Copy-PnPFile doesn’t work with root folders.
    If I specify a folder or a file, it works fine, but, just at the root, i’ve got an error :

    Copy-PnPFile : {“odata.error”:{“code”:”-2147467261,
    System.ArgumentNullException”,”message”:{“lang”:”fr-FR”,”value”:”Value cannot be null.\r\nParameter name: Null value
    for source item at https://darkana365.sharepoint.com/sites/SOGEPROM/Documents partages”}}}
    Au caractère C:\PnP\libraryContentCopyOnly.ps1:23 : 5
    + Copy-PnPFile -SourceUrl “Documents%20partages” -TargetUrl $Destin …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : WriteError: (:) [Copy-PnPFile], HttpRequestException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.CopyFile

    After a little search, i get this bug report on PnP : https://github.com/pnp/powershell/issues/410

    So, How did you get this script Worked ?

    Reply

Leave a Reply