Share via

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 "******@contoso.com" -RetentionPolicy "TwoWeeksRetention"

# Verify the Retention Policy

Get-Mailbox -Identity "******@contoso.com" | Select-Object RetentionPolicy
Exchange Online
Exchange Online

A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Andy David - MVP 160.3K Reputation points MVP Volunteer Moderator
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"

Was this answer helpful?


2 additional answers

Sort by: Most helpful
  1. 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!

    Was this answer helpful?

    0 comments No comments

  2. 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.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.