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?

copy sharepoint online site using pnp provioning template powershell

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

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

11 thoughts on “How to Copy a Site in SharePoint Online using PowerShell?

  • Hi, could you confirm if this is still working as explained? I used this solution back in May/June successfully, trying this process now results in the following: List structures are imported but not list data, Only empty document libraries are imported, no documents imported (No site assets docs nor Shared Document library docs) , Pages are no longer imported either. I think something major has broken in the PnP.Powershell module, I suspect due to a recent update.
    Do you get the same outcome when trying this now or is it only me? Thank you!

    Reply
    • Any update on this please?

      Reply
  • Hi! Thank you a lot for this content! How to copy also a configuration of content types? I have libraries that use document sets, but they do not copy and even if I set up them manually, shared columns are not working anymore for those document sets.

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

    Reply
  • so this work just for Subsite?

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

    Reply
  • So. What is the new procedure?

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

    Reply
  • Good post.
    I want to Apply-PnPProvisioningTemplate to a sub site of a new site collection. Can you please let me know how?

    Reply

Leave a Reply

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