Create a retention policy in Exchange Online using PowerShell

Marcus Tagtstrom 20 Reputation points
2024-04-09T08:24:49.0666667+00:00

How can I use PowerShell to create a retention policy that deletes emails older than two weeks for a specific account in Exchange Online? I want to ensure that only the intended account is affected by the policy. Additionally, how can I verify that the policy has been properly applied? Would the following script accomplish this?

# Connect to the tenant's Exchange Online using PowerShell

New-RetentionPolicy -Name "TwoWeeksRetention" -RetentionPolicyTagLinks "DeleteAfter14Days"

Set-Mailbox -Identity "example@contoso.com" -RetentionPolicy "TwoWeeksRetention"

# Verify the Retention Policy

Get-Mailbox -Identity "example@contoso.com" | Select-Object RetentionPolicy
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,186 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,068 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andy David - MVP 142.2K Reputation points MVP
    2024-04-09T11:58:41.2833333+00:00

    I would follow this and create a default tag that applies to the entire mailbox:

    https://learn.microsoft.com/en-us/exchange/security-and-compliance/messaging-records-management/create-a-retention-policy#step-1-create-a-retention-tag

    Example:

    New-RetentionPolicyTag -Name "DPT-Corp-Delete" -Type All -AgeLimitForRetention 14 -   RetentionAction DeleteAndAllowRecovery
    

    New-RetentionPolicy "RetentionPolicy-Corp" -RetentionPolicyTagLinks "DPT-Corp-Delete"

    Set-Mailbox "Morris" -RetentionPolicy "RetentionPolicy-Corp"

    Start-ManagedFolderAssistant -Identity "Morris"


2 additional answers

Sort by: Most helpful
  1. Marcus Tagtstrom 20 Reputation points
    2024-04-26T09:50:29.6133333+00:00

    Hi Andy,

    Thank you for your reply.

    I have tried your recommended solution but I can't see that e-mails older then 2 weeks are being deleted..

    Does it take a while before the policy starts deleting older e-mails or could it be something else that is being difficult? FYI, the M365 tenant where I'm applying this retention policy and it's retention policy tag doesn't have any other retention policy's that could interfere.


  2. Marcus Tagtstrom 20 Reputation points
    2024-04-29T06:48:50.3133333+00:00

    Hi Andy,

    I left it over the weekend and I can now see that all e-mails older then 14 days for the selected account had been deleted!

    Thank you for your assistance!

    0 comments No comments