Site policies in SharePoint Online help administrators manage the lifecycle of site collections by defining rules for automatic closure, deletion, and retention. Using PnP PowerShell, you can configure site policies efficiently to ensure governance and compliance.
This guide covers:
✔ Understanding Site Policies
✔ Listing Available Site Policies
✔ Applying a Site Policy to a Site Collection
✔ Removing a Site Policy
✔ Creating and Managing Custom Site Policies
Prerequisites
Before proceeding, ensure that you have:
SharePoint Admin or Global Admin permissions
PnP PowerShell module installed
Connected to SharePoint Online
Step 1: Install and Import PnP PowerShell Module
If you haven’t installed PnP PowerShell, install it using:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
Then, import the module:
Import-Module PnP.PowerShell
Step 2: Connect to SharePoint Online
To manage site policies, connect to SharePoint Online as an Admin:
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive
🔹 Replace "yourtenant"
with your SharePoint tenant name.
🔹 The -Interactive
flag prompts for authentication.
For App-based authentication, use:
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -ClientId "Your-App-Client-ID" -Tenant "yourtenant.onmicrosoft.com" -CertificatePath "Path\To\Certificate.pfx"
Step 3: List Available Site Policies
Before applying a policy, list the existing site policies available in the SharePoint Site Collection:
Get-PnPSitePolicy
This retrieves all available site policies configured in the SharePoint Online environment.
Step 4: Apply a Site Policy to a Site Collection
To apply a specific site policy to a site collection, use:
Set-PnPSitePolicy -Policy "Your Policy Name"
Replace "Your Policy Name"
with the actual site policy name retrieved from the previous step.
To apply a policy to a specific site, use:
Set-PnPSitePolicy -Url "https://yourtenant.sharepoint.com/sites/YourSite" -Policy "Your Policy Name"
Example: If your site policy is “Archive after 6 months”, apply it as:
Set-PnPSitePolicy -Url "https://yourtenant.sharepoint.com/sites/Marketing" -Policy "Archive after 6 months"
The policy will now be applied, and SharePoint will enforce the configured rules on the site.
Step 5: Remove a Site Policy from a Site Collection
If you want to remove a site policy from a site, use:
Clear-PnPSitePolicy -Url "https://yourtenant.sharepoint.com/sites/YourSite"
This removes the assigned site policy without affecting the site’s content.
Step 6: Create a Custom Site Policy
If you need to create a custom site policy, follow these steps:
1. Enable Site Policy Feature
Ensure that the Site Policy feature is enabled on the site collection:
Enable-PnPFeature -Identity "SitePolicy" -Scope Site
2. Create a New Site Policy
You must configure a custom policy manually in the SharePoint Admin Center, as PnP PowerShell currently does not support creating policies programmatically.
Follow these steps:
- Go to SharePoint Admin Center → Active Sites.
- Select the site where you want to configure the policy.
- Navigate to Site Settings → Site Policies.
- Click Create a New Policy and define rules for archiving, closing, and deleting sites.
Once created, you can use Step 4 to apply this policy using PowerShell.
Step 7: Verify the Site Policy Configuration
To check which policy is applied to a site collection, use:
Get-PnPSitePolicy
or
(Get-PnPTenantSite -Url "https://yourtenant.sharepoint.com/sites/YourSite").SitePolicy
This will display the current policy applied to the site.
Step 8: Disconnect PowerShell Session
Once the task is complete, disconnect the session:
Disconnect-PnPOnline
This prevents unauthorized access and ensures security.
Common Errors & Troubleshooting
Error | Possible Cause | Solution |
---|---|---|
No policies found | Site policies not enabled | Enable the Site Policy feature using Enable-PnPFeature |
Policy name not recognized | Incorrect policy name | Verify policy names using Get-PnPSitePolicy |
Access Denied | Insufficient permissions | Ensure you are a SharePoint Admin or Site Owner |
Command not recognized | PnP PowerShell module missing | Run Install-Module -Name PnP.PowerShell |
Cannot connect to SharePoint Online | Authentication issues | Use -Interactive login mode |