Site Template Export

This PnP PowerShell script automates the export of SharePoint Online sites as reusable templates. Create standardized site templates to ensure consistency and accelerate site provisioning across your organization.

Purpose

This script helps you:

  • Export existing sites as reusable templates
  • Create standardized site configurations for deployment
  • Backup site structures and settings for disaster recovery
  • Document and preserve organizational site designs

Prerequisites

  • PnP PowerShell module installed
  • Site collection administrator permissions
  • Connection to your SharePoint Online site
  • Sufficient storage space for template files
  • Export permissions on source sites

PowerShell Script

# Script to generate a PnP template that gets the main structural parts of the site.

# --- Configurable parameters ---
$SiteUrl = "https://tenantName.sharepoint.com/sites/siteName" # Add target site path
$CurrentDateTime = Get-Date -Format "yyyyMMddHHmmss"
$OutputFileName = "SiteTemplateTest.xml"
$PathToSave = "" # Add file path for output
$ClientId = "" # Add client id

$LogFilePath = Join-Path -Path $PathToSave -ChildPath "PnP_Script_Log_$CurrentDateTime.log" # File path for the log file

# --- Logging configuration ---
# Send all error messages to log
$ErrorActionPreference = "Stop" # Stops execution if an unhandled error occurs
Set-PnPTraceLog -On -LogFile $LogFilePath -Level Debug # Activate detailed logging for PnP


Try {
    Connect-PnPOnline -Url $SiteUrl -Interactive -ClientId $ClientId
}
Catch {
    Write-Error "Error connecting to SharePoint site: $($_.Exception.Message)"
    Exit
}

# --- Make sure the save path exists ---
If (-not (Test-Path $PathToSave)) {
    Write-Host "Creating directory $($PathToSave)..."
    New-Item -ItemType Directory -Path $PathToSave | Out-Null
}

# --- Export the PnP template ---
$OutputFilePath = Join-Path -Path $PathToSave -ChildPath $OutputFileName
Write-Host "Generating PnP template for lists, site columns, content types, pages..."

Try {
    Get-PnPSiteTemplate -Out $OutputFilePath `
        -Handlers "All" `
        -PersistMultiLanguageResources `
        -PersistBrandingFiles `
        -Force ` # Overwrites the file if it already exists

    Write-Host "Template successfully created in: $($OutputFilePath)"

    # Array of list names to add data rows for
    $ListsToIncludeData = @(
        "Events",
        "Feedback",
        "Tools"
    )

    # Loop through each list and add data rows to template
    ForEach ($ListName in $ListsToIncludeData) {
        Write-Host "Adding data rows for list: $ListName"
        Add-PnPDataRowsToSiteTemplate -Path $OutputFilePath `
            -List $ListName `
            -Query ""
    }

}
Catch {
    Write-Error "Error creating PnP Template: $($_.Exception.Message)"
}
Finally {
    Write-Host "Disconnecting from SharePoint Online."
    Disconnect-PnPOnline
    Set-PnPTraceLog -Off # Turn off the logging
}

Write-Host "Script finished."

Usage Notes

  • Large sites may take considerable time to export
  • Review exported template content before applying to other sites
  • Consider excluding sensitive data or user-specific content
  • Test exported templates in development environments first
  • Document template dependencies and requirements
  • Regular template exports help maintain backup strategies

Export Options

  • Full Site Export - Complete site structure and content
  • Configuration Only - Site settings without content
  • Selective Export - Specific components and features
  • PnP Template Format - Modern provisioning template

What Gets Exported

  • Site columns and content types
  • Lists and libraries configuration
  • Navigation and page layouts
  • Web parts and customizations (limited)
  • Site settings and features
  • Taxonomy and metadata structures