Site Template Apply
This PnP PowerShell script automates the application of site templates to SharePoint Online sites. Apply standardized site configurations, structures, and content to ensure consistency across your SharePoint environment.
Purpose
This script helps you:
- Apply standardized site templates across multiple sites
- Ensure consistent site structure and configuration
- Deploy organizational branding and layout templates
- Implement governance policies through template application
Prerequisites
- PnP PowerShell module installed
- Site collection administrator permissions
- Connection to your SharePoint Online site
- Existing site template file (.pnp or .xml)
- Appropriate permissions on target sites
PowerShell Script
# Script to apply a PnP and generate a new SharePoint Online site
# --- Configurable parameters ---
$TargetSiteUrl = "" # Add target site path
$TemplateFileName = "SiteTemplateTest.xml" # This is the name of the previously exported file
$ClientId = "" # Add client id
$TemplateFileDirectory = "" # Add file path where the template is stored
$CurrentDateTime = Get-Date -Format "yyyyMMdd_HHmmss"
$LogFilePath = Join-Path -Path $TemplateFileDirectory -ChildPath "PnP_Apply_Script_Log_$CurrentDateTime.log" # Path for error log
# --- Configuración del Logging ---
# Send all errors to log log
$ErrorActionPreference = "Continue" # Stops execution if unhandled erro occurs
Set-PnPTraceLog -On -LogFile $LogFilePath -Level Debug # Activate detailed logging for PNP
Try {
Write-Host "Connecting to the target SharePoint site: $TargetSiteUrl..."
# Connect to SharePoint Online
Connect-PnPOnline -Url $TargetSiteUrl -Interactive -ClientId $ClientId
}
Catch {
Write-Error "Error connecting to the SharePoint site: $($_.Exception.Message)"
Exit
}
# --- Make sure the save path exists ---
If (-not (Test-Path $TemplateFileDirectory)) {
Write-Error "The directory of the template doesn't exist: $($TemplateFileDirectory). Please, verify the path."
Exit
}
# --- Check the tempalte exists ---
$TemplateFilePath = Join-Path -Path $TemplateFileDirectory -ChildPath $TemplateFileName
If (-not (Test-Path $TemplateFilePath)) {
Write-Error "The template file doesn't exist: $($TemplateFilePath). Please, verify the path and the name of the file."
Exit
}
# --- Apply the PnP template ---
Write-Host "Applying the PnP template '$TemplateFileName' to site: $TargetSiteUrl..."
Try {
# Apply the template with the correct handlers
Invoke-PnPSiteTemplate -Path $TemplateFilePath `
-Handlers "All" `
-ClearNavigation
Write-Host "Template applied successfully: $($TargetSiteUrl)"
}
Catch {
Write-Error "Error applying the PnP template: $($_.Exception.Message)"
}
Finally {
Write-Host "Disconnect from SharePoint Online."
Disconnect-PnPOnline
Set-PnPTraceLog -Off # Turn off the logging
}
Write-Host "Script finished."
Usage Notes
- Backup target sites before applying templates
- Ensure template compatibility with target site version
- Review template contents and dependencies before application
- Test template application on development sites first
- Consider impact on existing content and customizations
- Verify permissions and security settings after application
Template Types
- PnP Template (.pnp) - Modern PnP provisioning templates
- XML Template (.xml) - Legacy SharePoint templates
- Site Script Templates - JSON-based modern templates