I would like to save a few SharePoint Online sites as template for future use with PowerShell. How can I do this? I found code snippets for 2013 but they do not seem to work, I'm missing methods, cannot retrieve the web object (no Get-SPWeb), thus no .SaveAsTemplate. What are my options? I have 100+ different sites to save.

Update: All my different sites that I want to make template of, are under a site collection. This is how far I am with the PnP Powershell:

$WebURL = "myroot"
Connect-PnPOnline –Url $WebURL -Credentials $credential
foreach ($web in Get-PnPSubWebs -Recurse) {
$url = $web.url
Connect-PnPOnline –Url $web.url –Credentials $credential
$filename = $url -replace $WebURL, "" -replace "/", "_" -replace ":", "|"
$filename += ".xml"
$path = "C:\Temp\$filename"
Get-PnPProvisioningTemplate -Force -Out $path -PersistBrandingFiles -PersistPublishingFiles

I get the templates, I can also use them, but the web parts are missing (xml view web parts, content query web parts)

  • Do you mean SPSites or SPWebs?
    – Jussi Palo
    Apr 18 '18 at 8:26
  • corrected, SPWeb. I would like to replicate the "Save Site as template" funcionality.
    – vilmarci
    Apr 18 '18 at 8:29

Take a look at the PnP PowerShell, namely the Get-PnPProvisioningTemplate, although I have a feeling it doesn't support SPWebs (although documentation talks of "webs").

So something like:

Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/publishing" -UseWebLogin

Get-PnPProvisioningTemplate -Out "C:\Path\to\template.xml" -PersistBrandingFiles -PersistPublishingFiles;
  • Thanks I will check if this works good for me or not. I was really looking for a way that ends up in the solution gallery and make the templates usable by the end users.
    – vilmarci
    Apr 18 '18 at 9:05
  • Looks like working, but this method does not save the web parts on the page. I checked the xml templates, there's no trace in them. Any ideas?
    – vilmarci
    Apr 20 '18 at 14:57
  • Sorry for the late feedback, this looks promising.
    – vilmarci
    Apr 24 '18 at 19:03

Site Provisioning with PnP PowerShell is good choice :

Get-PnPProvisioningTemplate -Out C:\temp\template.pnp -PersistBrandingFiles 
New-PnPTenantSite -Url 'https://tenant.sharepoint.com/sites/pnptempate' -Title 'New Site Collection' -Owner 'admin@domain.co.uk'
Connect-PnPOnline 'https://tenant.sharepoint.com/sites/pnptempate'
Apply-PnPProvisioningTemplate -Path C:\temp\template.pnp

Another approach you can use CSOM to meet your requirement .

Save Site as Template" using JSOM or CSOM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.