/commands/purview/retention-policy-and-label-cmdlets
Purview retention in PowerShell: New-RetentionCompliancePolicy, New-RetentionComplianceRule, New-ComplianceTag
- tool
- Microsoft Purview (Security & Compliance PowerShell)
- versions
- Security & Compliance PowerShell, ExchangeOnlineManagement 3.x
- os
- windows, linux, macos
In Security & Compliance PowerShell a retention policy is two objects: the policy (which locations it covers) and a rule (how long and what to do). Connect with Connect-IPPSSession, then create both:
New-RetentionCompliancePolicy -Name "Finance 7 years" `
-ExchangeLocation "kitty@contoso.com","scott@contoso.com" `
-SharePointLocation "https://contoso.sharepoint.com/sites/finance"
New-RetentionComplianceRule -Name "Finance 7 years rule" -Policy "Finance 7 years" `
-RetentionDuration 2555 -RetentionComplianceAction KeepAndDelete -ExpirationDateOption ModificationAgeInDaysThis keeps content for 2555 days (7 years) after it was last modified, then deletes it. You need a Purview role that covers retention, for example the Compliance Administrator role.
Rule values
| Parameter | Values |
|---|---|
| -RetentionDuration | Number of days, or Unlimited |
| -RetentionComplianceAction | Keep, Delete, KeepAndDelete |
| -ExpirationDateOption | CreationAgeInDays, ModificationAgeInDays (not for Teams rules) |
| -PublishComplianceTag | Name of a retention label to publish (label policies only) |
Locations
New-RetentionCompliancePolicy takes -ExchangeLocation, -SharePointLocation, -OneDriveLocation, -ModernGroupLocation (Microsoft 365 groups), -PublicFolderLocation, -TeamsChannelLocation, -TeamsChatLocation, -SkypeLocation and -AdaptiveScopeLocation. Use All for every mailbox or site:
New-RetentionCompliancePolicy -Name "Mail 3 years" -ExchangeLocation All
New-RetentionComplianceRule -Policy "Mail 3 years" -RetentionDuration 1095 -RetentionComplianceAction KeepTeams chats and channel messages go in a policy of their own, without other locations.
Retention labels
A retention label is a ComplianceTag. Create it, then publish it with a label policy (a retention policy whose rule uses -PublishComplianceTag):
New-ComplianceTag -Name "HR Content" -RetentionAction Keep -RetentionDuration 1825 -RetentionType ModificationAgeInDays
New-RetentionCompliancePolicy -Name "Publish HR labels" -ExchangeLocation All -SharePointLocation All
New-RetentionComplianceRule -Policy "Publish HR labels" -PublishComplianceTag "HR Content"Check and change
Get-RetentionCompliancePolicy -DistributionDetail | Format-List Name,Enabled,DistributionStatus
Get-RetentionComplianceRule -Policy "Finance 7 years" | Format-List Name,RetentionDuration,RetentionComplianceAction
Set-RetentionCompliancePolicy -Identity "Finance 7 years" -AddExchangeLocation "anna@contoso.com"
Get-ComplianceTag | Format-Table Name,RetentionAction,RetentionDurationUse -DistributionDetail when you read DistributionStatus: without it, Microsoft notes that the status and the location properties are inaccurate. Distribution to all locations takes time after a change.
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/exchangepowershell/new-retentioncompliancepolicy
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/exchangepowershell/new-retentioncompliancerule
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/exchangepowershell/new-compliancetag
- learn.microsoft.comlearn.microsoft.com/en-us/purview/retention-cmdlets