Managing SharePoint Retention Policies using PnP PowerShell

Loading

Retention policies in SharePoint Online help organizations comply with regulatory requirements by preserving or deleting data after a set period. Using PnP PowerShell, you can automate the management of these policies efficiently.


Step 1: Install and Connect PnP PowerShell

Before running any commands, install PnP PowerShell if not already installed:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -Force

Then, connect to SharePoint Online:

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

Connected to SharePoint Online!


Step 2: Get Existing Retention Policies

To view the retention policies in your tenant:

Get-PnPRetentionCompliancePolicy

This will output details of all retention policies, including policy names, descriptions, and enforcement status.

Retention policies retrieved!


Step 3: Create a New Retention Policy

To create a new retention policy that applies to SharePoint:

New-PnPRetentionCompliancePolicy -Name "HR Documents Retention" `
-Comment "Retains HR documents for 5 years before deletion" `
-ExchangeLocation "All" `
-SharePointLocation "All" `
-OneDriveLocation "All" `
-RetentionComplianceRule @{ RetentionDuration = 1825; RetentionAction = "Delete" }

Parameters Explained:

  • RetentionDuration = 1825 → Retains data for 5 years (365 x 5)
  • RetentionAction = “Delete” → Deletes content after 5 years
  • SharePointLocation = “All” → Applies to all SharePoint sites
  • OneDriveLocation = “All” → Applies to all OneDrive accounts

Retention policy created!


Step 4: Assign a Retention Policy to Specific Sites

If you want to apply a policy only to specific sites, retrieve the site URLs and assign them:

$PolicyName = "HR Documents Retention"
$SiteUrls = @("https://yourtenant.sharepoint.com/sites/HR", "https://yourtenant.sharepoint.com/sites/Finance")

Set-PnPRetentionCompliancePolicy -Identity $PolicyName -SharePointLocation $SiteUrls

Retention policy applied to specific SharePoint sites!


Step 5: Modify an Existing Retention Policy

To update an existing policy, such as changing the retention duration:

Set-PnPRetentionCompliancePolicy -Identity "HR Documents Retention" `
-RetentionComplianceRule @{ RetentionDuration = 3650; RetentionAction = "RetainAndDelete" }

🔹 RetentionAction = “RetainAndDelete” → Keeps data for 10 years before deleting.

Retention policy updated!


Step 6: Remove a Retention Policy

To delete an existing retention policy:

Remove-PnPRetentionCompliancePolicy -Identity "HR Documents Retention" -Confirm:$false

Retention policy removed!


Step 7: Check Retention Policy Assignments on a Site

To verify if a retention policy is applied to a SharePoint site:

$SiteUrl = "https://yourtenant.sharepoint.com/sites/HR"
Get-PnPRetentionCompliancePolicy | Where-Object { $_.SharePointLocation -contains $SiteUrl }

Retention policy assignments verified!

Leave a Reply

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