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?

Have you ever wanted to copy a document library in SharePoint Online? Perhaps you want to create a new library with the same structure and content as an existing one, or you need to duplicate a library’s contents for archiving or testing purposes. Whatever the reason, it’s easy to do. This article will show you how to use PowerShell to copy a document library on the SharePoint Online site.

There are no direct ways to copy a document library in SharePoint Online. However, You can use this list template 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 -Interactive

    #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. To copy a document library to another site, use: How to Copy a Document Library to another Site in 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!

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

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

    So, How did you get this script Worked ?


Leave a Reply