Introduction
In this article, we will learn how to copy a SharePoint site schema from one site collection or tenant to another using PowerShell. Recently, I was working on a project where we needed to migrate sites to different SharePoint tenants and setup multiple environments. The source site had several lists and libraries with many columns associated with it.
I used PowerShell script to copy these sites and to minimize manual efforts, errors and reuse script for similar requirements. Below PowerShell script exports the source site template and then creates a new SharePoint site using the exported template. It will create all lists and libraries (No content) in the destination SharePoint tenant. You have to just replace the site URLs and path to use this script.
Prerequisites
- Windows PowerShell
- SharePoint admin access
Below are the steps to copy SharePoint site using PowerShell,
Step 1
PowerShell script to export site template from the source SharePoint site using Windows PowerShell.
#Parameters
$SourceSiteURL = "https://qhdm.sharepoint.com/sites/MyTeamsites"
$XMLTemplatePath = "C:\Site Templates\DemoSiteTemplate.xml"
#Connect to source SharePoint
Connect-PnPOnline -Url $SourceSiteURL -Interactive
#Export site template to specified path
Get-PnPSiteTemplate -Out ($XMLTemplatePath)
#Disconnect source SharePoint
Disconnect-PnPOnline
This script will connect to the source SharePoint site and export the XML site template into the provided local folder path. Now, you have to create a blank SharePoint site in the destination tenant and use that site URL in the below script to apply the site template.
Step 2
PowerShell to apply site template to destination SharePoint site using Windows PowerShell.
#Parameters
$DestinationSiteURL = "https://qhdm.sharepoint.com/sites/DestinationDemoSites"
$XMLTemplatePath = "C:\Site Templates\DemoSiteTemplate.xml"
#Connect to PnP Online
Connect-PnPOnline -Url $DestinationSiteURL -Interactive
#Apply SharePoint template
Invoke-PnPSiteTemplate -Path $XMLTemplatePath -ClearNavigation
#Disconnect source SharePoint
Disconnect-PnPOnline
This script will apply the exported site template to the destination SharePoint site, creates all lists, libraries, and its associated columns.
Conclusion
In this article, you have learned how to copy SharePoint sites using a simple PnP PowerShell script. You can use this script to copy sites including list, libraries with their associated field to different site collections or SharePoint tenants.