Forum Discussion
Apply Site Policy to Multiple Sites with PnP PowerShell
I have 1000's of sites which I wish to apply a site policy to. This page describes how to create a policy in the content type hub, and then activate the Site Policy feature and apply the created policy to a site using PnP PowerShell - the 2nd script on the page.
I am a PowerShell/ PnP PowerShell novice, but was able to activate the Site Policy feature for a test site and apply a policy using the script (although when I run that 2nd script I get a "A parameter cannot be found that matches parameter name 'Web'." error for line 8, but it still works).
What I'd like to do next is add a policy to multiple sites - e.g. from a list of site url's - in one go: is this possible?
Yeah that's pretty straight forward, question is where is the list? Is it a SharePoint list or a Excel/CSV file ? as you need to import it
Here is a base code you can use:
$AdminSiteURL = "https://contoso-admin.sharepoint.com" Try { #Connect to Admin Center Connect-PnPOnline -Url $AdminSiteURL -Interactive #Get All Site collections (You will change this later on) $SitesCollection = Get-PnPTenantSite #Loop through each site collection ForEach($Site in $SitesCollection) { Enable-PnPFeature -Identity "ID OF YOUR FEATURE" Write-host -F Green $Site.Url } } Catch { write-host -f Red "Error:" $_.Exception.Message }
Yeah that's pretty straight forward, question is where is the list? Is it a SharePoint list or a Excel/CSV file ? as you need to import it
Here is a base code you can use:
$AdminSiteURL = "https://contoso-admin.sharepoint.com" Try { #Connect to Admin Center Connect-PnPOnline -Url $AdminSiteURL -Interactive #Get All Site collections (You will change this later on) $SitesCollection = Get-PnPTenantSite #Loop through each site collection ForEach($Site in $SitesCollection) { Enable-PnPFeature -Identity "ID OF YOUR FEATURE" Write-host -F Green $Site.Url } } Catch { write-host -f Red "Error:" $_.Exception.Message }
- Russ ThomsonBrass Contributor
NicolasKheirallah - perfect: works like a charm. Thank you! Bonus question: do you know how to automatically apply a policy to new sites when they're created - is this maybe something which can be set up in the Central Admin site (which unfortunately I don't have access to)?
- No but you can create a site template and apply it each time you create a site
https://learn.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-design-json-schema#activate-a-feature