Forum Discussion

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

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...
  • NicolasKheirallah's avatar
    Jul 10, 2023

    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