How to Copy a Site in SharePoint Online using PowerShell?
Requirement: Copy a sub-site in SharePoint Online using PnP Provisioning Template.
Site templates are a great way to make a “template” and create a copy of a site, including its structure and content. However, Microsoft has removed the “Save as Template” feature in modern site templates and group associated team sites or communication sites. So, How to make a copy of a site in SharePoint Online?
How to copy a Site Collection in SharePoint Online using PowerShell?
PnP PowerShell is a powerful way to provision site artifacts in SharePoint Online. We can use it to duplicate a subsite, and now, let’s use it to copy a subsite using the PnP provisioning template.
- Step 1: Connect to the Source site and Get its Schema
- Step 2: Create a New Subsite
- Step 3: Import the Schema to the Target Site
Step 1: Get the Source Site Schema XML
Create your site using the SharePoint UI, do the necessary customization. Once the site is ready to use as a template, Connect to the source site and get the site schema in XML format using Get-PnPSiteTemplate cmdlet.
#Set variables
$SiteURL = "https://crescent.sharepoint.com/sites/projects/london"
$SchmaXMLPath = "C:\Temp\SiteSchema.xml"
#Connect to PnP Online
Connect-PnPOnline -Url $siteUrl -Interactive
#Get Site Schema
Get-PnPSiteTemplate -Out ($SchmaXMLPath) -PersistBrandingFiles -PersistPublishingFiles
This cmdlet gets all artifacts from the source site, such as content types, site columns, term store, list and libraries, theme, pages, etc., into the given template XML file. We also have a switch -includesitegroups to include site security so that your target site will have unique permissions.
Step 2: Create a New Site Collection or Subsite
Create a new subsite of the same template as the source site.
#Set variables
$SiteURL = "https://crescent.sharepoint.com/sites/projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Create a new subsite
$NewWeb = New-PnpWeb -Title "Project Morocco" -Url "morocco" -Description "Team Site for Morocco Project" -Locale 1033 -Template "STS#3"
The next step is to get the target site and apply the template to it. The exported XML file can be applied to any other site to turn them into a copy of the original site.
Step 3: Apply the Site Schema into Target Site with Invoke-PnPSiteTemplate
Once the subsite is created, apply it to the source site and get the site schema in XML format.
#Set variables
$SiteURL = "https://crescent.sharepoint.com/sites/projects/morocco"
$SchmaXMLPath = "C:\Temp\SiteSchema.xml"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Apply Pnp Provisioning Template
Invoke-PnPSiteTemplate -Path $SchmaXMLPath -ClearNavigation
Please note, the PnP Provisioning template method only extracts the site template without its contents. If you need to copy the site with its content, You need to do it with PowerShell scripts or with any migration tool. Also, you can’t extract the site template of a root site and apply it on subsites! Here is the PnP provisioning documentation: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/pnp-provisioning-framework
Hi,
i have a problem , i am using “CONTENT TYPE” to hide my “Title” + i have several lists who are “lookups”
i have tried like a 100 diffrent ways but cant get the “content type” config to go over so all of my data comes “wrong” , the structure is wrong, what to do ?
so this work just for Subsite?
Hi. Thanks for the post. Do you have a script to do this for multiple sites? Meaning using one site to copy as several new sites.
Thanks!
You can create Multiple sites from a CSV and apply the site template to any number of sites!
How to Bulk Create Multiple SharePoint Online Sites?
So. What is the new procedure?
Gourav Apply-PnPProvisioningTemplate is not available now. Please visit https://pnp.github.io/powershell/cmdlets/Add-PnPAlert.html and look after new cmdlets. They changed procedure of cloning SharePoint sites.
Yes! It is replaced with “Invoke-PnPSiteTemplate” cmdlet.
Good post.
I want to Apply-PnPProvisioningTemplate to a sub site of a new site collection. Can you please let me know how?