SharePoint (2003 thru Online): Add/Remove SharePoint sites (bulk list) to Retention Policy thru PowerShell.

Monday, May 6, 2024

Add/Remove SharePoint sites (bulk list) to Retention Policy thru PowerShell.

We have a requirement to add 150 sites to the exclusion list of our retention policy. While we have traditionally accomplished this by manually editing the retention policy and adding the sites, this method is not feasible for such a large number of sites.

NOTE: 
Excluding sites from policy requires time, ranging from 10 minutes to 24 hours, to take effect.

Edit Retention Policy > Click on Edit (under Excluded) and add the sites.



Instead, we would like to use a PowerShell script to automate the process. This will allow us to add all the sites quickly and efficiently.

List all the sites in a CSV file with URL as Header (As shown below).

NOTE: I had to exclude 150 sharepoint sites from the retention policy, but it only allows 100 at a time. So I split them into two CSV files, one with 100 and one with 50. Then I ran the Powershell Script for each CSV file separately.


Below is the PowerShell Script.
NOTE: This activity requires certain permissions, such as Compliance Admin or Global Admin.
____________________________________________________________________________________
#Variables
$PolicyName = "FilesRetentionPolicy"
$CSVPath = "D:\DEV_Sites.csv"

#Get the CSV file contents, column URL to the array.
[array]$excludesites = Import-CSV -Path $CSVPath | Select -ExpandProperty URL

#Connect to Compliance Center through Exchange Online Module
Connect-IPPSSession -UserPrincipalName spadmin@gurram.onmicrosoft.com

#Get the Policy
$Policy = Get-RetentionCompliancePolicy -Identity $PolicyName

#Add SPO Sites (array) to Retention Policy
Set-RetentionCompliancePolicy -Identity $Policy.Name -AddSharePointLocationException $excludesites

----------------Use Remove when needed----------------
#Remove SPO Sites (array) from Retention Policy
Set-RetentionCompliancePolicy -Identity $Policy.Name -RemoveSharePointLocationException $excludesites
---------------------------------------------------------------------

#Disconnect the Exchange Online Module
Disconnect-ExchangeOnline
____________________________________________________________________________________
After running the PowerShell Script successfully, you can manually verify the added sites in Retention Policy.


Click on Edit in the above screen. You will see as shown below.


No comments:

Post a Comment