Office 365: Change default e-mail retention to 1 year

Modified on Wed, 12 Nov at 4:05 PM

<#
.SYNOPSIS
This script creates a new retention tag and adds it to the Default MRM Policy in Exchange Online.
It correctly removes any existing 'Default' tag from the policy.

.DESCRIPTION
1. Creates a "Default" retention tag that moves items older than 1 year (365 days) to the archive.
2. Finds the "Default MRM Policy".
3. Finds and removes the *existing* 'Default' tag from that policy.
4. Adds the new 'Default' tag to the policy.

.NOTES
You must be connected to Exchange Online PowerShell before running this script.
(Use Connect-ExchangeOnline)
#>

# --- Connect to Office 365 ---
Connect-ExchangeOnline

# --- Step 1: Define and create the new tag properties ---
$TagName = "Default 1 year move to archive"
$AgeLimit = 365 # 365 days = 1 year
$TagAction = "MoveToArchive"
$TagType = "All" # This applies to all untagged items in the mailbox

Write-Host "Checking for existing retention policy tag: $TagName..."
$ExistingTag = Get-RetentionPolicyTag -Identity $TagName -ErrorAction SilentlyContinue

if ($null -eq $ExistingTag) {
    Write-Host "Tag not found. Creating retention policy tag: $TagName..."
    New-RetentionPolicyTag -Name $TagName -Type $TagType -AgeLimitForRetention $AgeLimit -RetentionAction $TagAction
    Write-Host "Successfully created tag."
} else {
    Write-Host "Tag '$TagName' already exists."
}
Write-Host "---"

# --- Step 2: Add new tag and remove old default tag from the Default MRM Policy ---
$PolicyName = "Default MRM Policy"

Write-Host "Updating policy '$PolicyName'..."

# Get the policy
$Policy = Get-RetentionPolicy -Identity $PolicyName
if ($null -eq $Policy) {
    Write-Host "ERROR: Policy '$PolicyName' not found."
    # Use 'break' or 'return' if this is part of a larger function
    return
}

# Get all retention tags in the organization
$AllTags = Get-RetentionPolicyTag

# Get the names of the tags currently on the policy
$PolicyTagNames = $Policy.RetentionPolicyTagLinks

# Get the full tag objects for the tags on the policy
$PolicyTags = $AllTags | Where-Object { $PolicyTagNames -contains $_.Name }

# Find the *old* default tag (one that is 'All' type but not our new tag)
$OldDefaultTag = $PolicyTags | Where-Object { $_.Type -eq 'All' -and $_.Name -ne $TagName }

# Create the new list of tags for the policy, starting from the current list
$NewTagList = $Policy.RetentionPolicyTagLinks

if ($null -ne $OldDefaultTag) {
    Write-Host "Found existing default tag: $($OldDefaultTag.Name). It will be removed."
    # Remove the old default tag from the list
    $NewTagList = $NewTagList | Where-Object { $_ -ne $OldDefaultTag.Name }
} else {
    Write-Host "No other 'Default' tag found to remove."
}

# Add the new tag to the list (if it's not already there)
if ($NewTagList -notcontains $TagName) {
    Write-Host "Adding new default tag: $TagName"
    $NewTagList = $NewTagList + $TagName
} else {
    Write-Host "New tag '$TagName' is already on the policy."
}

# Set the policy with the updated list of tags
Write-Host "Applying new tag list to policy..."
Set-RetentionPolicy -Identity $PolicyName -RetentionPolicyTagLinks $NewTagList

Write-Host "Successfully updated policy '$PolicyName'."
Write-Host "---"
Write-Host "Script completed."

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article