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

The 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 an 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 the PnP Provisioning Engine. Here is the PowerShell script:

Use PnP Provisioning Template Method to Copy a List

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

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/"
$ListName = "Project Tasks"
$TemplateFile = "C:\Temp\ListSchema.xml"

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

#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 }
$Templates.Lists.Clear()
$Templates.Lists.Add($ListTemplate)
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 to the Target Site
Once we have the template XML file ready, we can import it to any site collection, as:

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Operations/"
$TemplateFile = "C:\Temp\ListSchema.xml"

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

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

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

If you want to copy list items between lists, use: How to Copy List Items Between Lists in SharePoint Online using PowerShell?

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 = "https://crescent-admin.sharepoint.com"
$ListURL = "https://crescent.sharepoint.com/sites/marketing/Lists/Source"
$TargetSiteURL = "https://crescent.sharepoint.com/sites/procurement/"

#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

Alternatively, you can use “Creating a list from an Existing List” or PowerShell methods to copy a list or document library to another site. How to Copy a List to another site in SharePoint Online?

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

8 thoughts on “SharePoint Online: Copy List or Document Library to Another Site using PnP PowerShell

  • Whenever I need to do anything in PowerShell, only this site comes to my mind.

    I am trying to migrate the SharePoint library folder and files structure to another site in PnP site provisioning but unfortunately all the bloggers are talking about list content only.

    Any idea how to do that in PnP provisioning ?

    Reply
  • getting the same error. Anyone get this to work?

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

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

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

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

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

    Reply

Leave a Reply

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