Entra / Microsoft 365 · Compliance & audit
Report SharePoint site retention
Reports retention policies and rules that apply to SharePoint Online and OneDrive for Business locations in the tenant.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-IPPSSession
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
$Report = [System.Collections.Generic.List[Object]]::new()# Fetch a set of retention policies that apply to SharePoint and aren't to publish labels$Policies = (Get-RetentionCompliancePolicy -ExcludeTeamsPolicy -DistributionDetail -RetentionRuleTypes | ? {$_.SharePointLocation -ne $Null -and $_.RetentionRuleTypes -ne "Publish"})ForEach ($P in $Policies) {$Duration = $NullWrite-Host "Processing retention policy" $P.Name$Rule = Get-RetentionComplianceRule -Policy $P.Name$Settings = "Simple"$Duration = $Rule.RetentionDuration# Check whether a rule is for advanced settings - either a KQL query or sensitive data typesIf (-not [string]::IsNullOrWhiteSpace($Rule.ContentMatchQuery) -and -not [string]::IsNullOrWhiteSpace($Rule.ContentMatchQuery)) {$Settings = "Advanced/KQL" }Elseif (-not [string]::IsNullOrWhiteSpace($Rule.ContentContainsSensitiveInformation) -and -not [string]::IsNullOrEmpty($Rule.ContentContainsSensitiveInformation)) {$Settings = "Advanced/Sensitive Data" }# Handle retention policy that simply retains and doesn't do anything elseIf ($Rule.RetentionDuration -eq $Null -and $Rule.ApplyComplianceTag -ne $Null) {$Duration = (Get-ComplianceTag -Identity $Rule.ApplyComplianceTag | Select -Expandproperty RetentionDuration) }$RetentionAction = $Rule.RetentionComplianceActionIf ([string]::IsNullOrEmpty($RetentionAction)) {$RetentionAction = "Retain" }If ($P.SharePointLocation.Name -eq "All") {$ReportLine = [PSCustomObject][Ordered]@{PolicyName = $P.NameSiteName = "All SharePoint Sites"SiteURL = "All SharePoint Sites"RetentionTime = $Rule.RetentionDurationDisplayHintRetentionDuration = $DurationRetentionAction = $RetentionActionSettings = $Settings}$Report.Add($ReportLine) }If ($P.SharePointLocationException -ne $Null) {$Locations = ($P | Select -ExpandProperty SharePointLocationException)ForEach ($L in $Locations) {$Exception = "*Exclude* " + $L.DisplayName$ReportLine = [PSCustomObject][Ordered]@{PolicyName = $P.NameSiteName = $ExceptionSiteURL = $L.Name }$Report.Add($ReportLine) }}ElseIf ($P.SharePointLocation.Name -ne "All") {$Locations = ($P | Select -ExpandProperty SharePointLocation)ForEach ($L in $Locations) {$ReportLine = [PSCustomObject][Ordered]@{PolicyName = $P.NameSiteName = $L.DisplayNameSiteURL = $L.NameRetentionTime = $Rule.RetentionDurationDisplayHintRetentionDuration = $DurationRetentionAction = $RetentionActionSettings = $Settings}$Report.Add($ReportLine) }}}$Report | Sort SiteName| Format-Table PolicyName, SiteName, RetentionDuration, RetentionAction, Settings -AutoSize
Attribution
Author
Office365itpros