Applying Site Policies in Bulk using PnP PowerShell

Loading

Site Policies in SharePoint help organizations manage site lifecycles by automatically archiving or deleting inactive sites. They enforce governance and ensure efficient storage utilization. PnP PowerShell allows administrators to apply these policies in bulk, automating the management process across multiple site collections.

In this guide, we will cover:

  • Understanding Site Policies
  • Creating and Managing Site Policies
  • Applying Site Policies in Bulk using PnP PowerShell
  • Automating the process with a scheduled script

Prerequisites

Install PnP PowerShell

Install-Module -Name PnP.PowerShell -Force -AllowClobber

Connect to SharePoint Online

Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive

Admin Permissions
You need SharePoint Admin or Global Admin permissions to manage site policies.


Step 1: Check Existing Site Policies

Before applying site policies, list all existing policies in a SharePoint Online site:

Get-PnPSitePolicy

🔹 This will return all available Site Policies in the connected site.


Step 2: Create a New Site Policy

If you don’t have an existing policy, create one using the SharePoint Admin Center or with PnP PowerShell:

New-PnPSitePolicy -Name "Auto Expire in 180 Days" -Description "This policy will close the site after 180 days of inactivity."

This creates a policy that automatically closes sites after 180 days.


Step 3: Apply a Site Policy to a Single Site

To apply a Site Policy to a specific SharePoint site:

Set-PnPSitePolicy -Policy "Auto Expire in 180 Days"

This applies the policy “Auto Expire in 180 Days” to the connected site.


Step 4: Apply Site Policies in Bulk from a CSV File

If you need to apply site policies to multiple sites, create a CSV file (e.g., Sites.csv) with the following format:

SiteURLPolicyName
https://yourtenant.sharepoint.com/sites/HRAuto Expire in 180 Days
https://yourtenant.sharepoint.com/sites/ITAuto Expire in 365 Days
https://yourtenant.sharepoint.com/sites/SalesArchive After 2 Years

Run the Bulk Script

powershellCopyEdit# Import CSV File
$sites = Import-Csv -Path "C:\Sites.csv"

foreach ($site in $sites) {
    try {
        # Connect to the Site Collection
        Connect-PnPOnline -Url $site.SiteURL -Interactive

        # Apply the Site Policy
        Set-PnPSitePolicy -Policy $site.PolicyName

        Write-Host "Successfully applied policy '$($site.PolicyName)' to $($site.SiteURL)" -ForegroundColor Green
    }
    catch {
        Write-Host "Failed to apply policy to $($site.SiteURL): $_" -ForegroundColor Red
    }
}

Automates applying Site Policies across multiple sites from a CSV file.


Step 5: Remove a Site Policy

If you need to remove a Site Policy from a site:

Remove-PnPSitePolicy

This removes any active Site Policy applied to the connected site.

For bulk removal from multiple sites in a CSV file:

$sites = Import-Csv -Path "C:\Sites.csv"

foreach ($site in $sites) {
try {
Connect-PnPOnline -Url $site.SiteURL -Interactive
Remove-PnPSitePolicy
Write-Host "Removed Site Policy from $($site.SiteURL)" -ForegroundColor Yellow
}
catch {
Write-Host "Failed to remove policy from $($site.SiteURL): $_" -ForegroundColor Red
}
}

Removes Site Policies in bulk from multiple sites.


Step 6: Schedule the Script for Automation

1️⃣ Open Task Scheduler
2️⃣ Click Create Basic Task
3️⃣ Set Trigger: Daily/Weekly
4️⃣ Set Action: Start a Program
5️⃣ In Program/script, enter:

-ExecutionPolicy Bypass -File "C:\YourScriptPath\ApplySitePolicies.ps1"

6️⃣ Click Finish

The script now runs automatically based on the schedule.


Troubleshooting Common Issues

Issue 1: Policy Not Found

  • Run Get-PnPSitePolicy to verify the correct policy name.
  • Ensure the policy exists before applying it.

Issue 2: Connection Errors

  • Check if you have Admin permissions.
  • Use -Interactive for authentication: powershellCopyEditConnect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive

Issue 3: CSV File Errors

  • Ensure CSV file has the correct column names: SiteURL and PolicyName.
  • Check for extra spaces or formatting issues.

Leave a Reply

Your email address will not be published. Required fields are marked *