SharePoint Online: Copy List or Document Library to Another Site using PnP PowerShell

Requirement: Copy Document Library to another site using PowerShell

SharePoint Online: Copy Document Library to Another site using PnP PowerShell

PnP provisioning engine lets us copy site objects such as lists and libraries, site columns, content types, composed looks, pages, etc. We can export existing customization to XML file and use it as a template to create new objects from it. We can copy lists and libraries between SharePoint Online site collections with the help of PnP Provisioning engine. Here is the PowerShell script:

Use PnP Provisioning Template Method to Copy List

Step 1: Get the List Schema from Source Site
With PnP provisioning engine, we need to extract the list schema first:

#Config Variables
$SiteURL = ""
$ListName = "Project Tasks"
$TemplateFile = "C:\Temp\ListSchema.xml"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get the List schema as Template and export to a File
$Templates = Get-PnPSiteTemplate -OutputInstance -Handlers Lists
$ListTemplate = $Templates.Lists | Where-Object { $_.Title -eq $ListName }
Save-PnPSiteTemplate -InputInstance $Templates -Out $TemplateFile

This script creates an XML template file that contains all list schema from the SharePoint Online site, and then filters to get the specific list.

Step 2: Apply the Template in the Target Site
Once we have the template XML file ready, we can import it to any site collection, as:

#Config Variables
$SiteURL = ""
$TemplateFile = "C:\Temp\ListSchema.xml"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

Write-Host "Creating List from Template..."
Invoke-PnPSiteTemplate -Path $TemplateFile

This PowerShell script copies list or document library from source site to the destination site collection.

Copy List Structure using Site Designs and Site Scripts

Here is how you can copy list structure using site design and scripts with PowerShell

#Define Parameters
$AdminCenterURL = ""
$ListURL = ""
$TargetSiteURL = ""

#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -credential (Get-Credential)

#Get the list schema
$ListSchema = Get-SPOSiteScriptFromList -ListUrl $ListURL

#Add list schema as Site Script 
$SiteScript = Add-SPOSiteScript -Title "Project Tasks List" -Content $ListSchema

#Create a Site Design
$SiteDesign = Add-SPOSiteDesign -Title "Provision Project Tasks List" -WebTemplate 64 -SiteScripts $SiteScript.Id

#Apply Site Design to site
Invoke-SPOSiteDesign -Identity $SiteDesign.Id -WebUrl $TargetSiteURL

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 “SharePoint Online: Copy List or Document Library to Another Site using PnP PowerShell

  • July 7, 2021 at 3:05 PM

    is this still valid code..?
    as Get-PnPProvisioningTemplate no more available i guess

    • July 18, 2021 at 4:36 PM

      Command names have changed. Use ‘Get-PnPSiteTemplate’ and ‘Invoke-PnPSiteTemplate’

      • September 9, 2021 at 1:34 PM

        Yes, If you have upgraded to the new PnP Powershell module, These cmdlets are replaced now!

  • April 14, 2021 at 4:03 PM

    Error on line 13 when getting the template:

    Exception calling “Add” with “1” argument(s): “Object reference not set to an instance of an object.”

    Any ideas?

  • February 12, 2021 at 7:03 AM

    Is there a way to also move the content of said library and it’s permissions?
    Many thanks!


Leave a Reply