Apply Retention Policies To Exchange Server 2016 and Online Using PowerShell

In this article, I’ll show you how to apply mailbox retention policies to Exchange Server 2016 and Online Users Mailboxes using PowerShell.

Retention Policies

Allows us to control and manage folders and emails Inside mailboxes running on Exchange Server 2016, 2013 and Online.

But controlling folders and email we can set policies to delete email older than X days and move them to archive storage.

In my case, I’m going to apply a retention policy that deleted emails older than 30 days that are located In the Deleted Items folder.

Step One – Find Users Without Retention Policy

The first step In my process Is to find the users that they don’t have a policy applied to them, Luckily I had written about this In the past and I will reuse my code and export the users.

To export the list of users without a retention policy to a CSV file, I'll run the code below

Get-Mailbox -ResultSize unlimited | where{$_.retentionpolicy -eq $null} | select name,alias | export-csv ret.csv -NoTypeInformation

Below you can see my CSV file

Apply Retention Policy

The final stage In the process will be Importing the CSV file and apply my policy (called Deleted Items Policy)

Import-Csv .\ret.csv | foreach {Set-Mailbox $_.alias -RetentionPolicy "Deleted Items Policy"}

By default, the policy will apply after 24 hours to the mailbox but If you want it to start immediately run the line below:

Import-Csv .\ret.csv | foreach {get-Mailbox $_.alias | Start-ManagedFolderAssistant }
Apply to all users

In some environments, I like to use the script below and run every few days and apply it to all users.

Get-Mailbox -ResultSize unlimited | where{$_.retentionpolicy -eq $null} | Set-Mailbox -RetentionPolicy "Deleted Items Policy"