Forum Discussion
Russ Thomson
Jul 07, 2023Brass Contributor
Apply Site Policy to Multiple Sites with PnP PowerShell
I have 1000's of sites which I wish to apply a site policy to. https://www.sharepointdiary.com/2019/08/sharepoint-online-apply-site-policy-using-powershell.html describes how to create a policy in th...
- Jul 10, 2023
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 }
Jul 10, 2023
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 Thomson
Jul 11, 2023Brass 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)?
- Jul 11, 2023No 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- Russ ThomsonJul 12, 2023Brass ContributorVery good - thanks Nicolas!