Forum Discussion

Russ Thomson's avatar
Russ Thomson
Brass Contributor
Jul 07, 2023

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?

  • Russ Thomson 

     

    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 

     

    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
    }

     

Resources