Search code examples
powershellscriptingoffice365exchange-server

Powershell to be executed for Deleting Exchange Shared Mailbox email older than one month old from today's date


I need to delete the Shared Mailbox email in the Inbox folder that is reaching about 420,000+ older than 1-month-old from today date.

Get-Mailbox -Filter '(RecipientTypeDetails -eq "SharedMailbox")' | Search-Mailbox '[email protected]' -TargetMailbox '[email protected]' -TargetFolder 'Inbox' -SearchQuery "kind:Email AND Received:"18/10/2018..1/1/1970"" -DeleteContent

The script that I have found does not have the timestamp capability, and I am not sure how to modify it, so I can run this script as the scheduled task every month.


Solution

  • You could refer to the below code:

    $DT = (Get-Date).AddDays(-30).ToString("d")
    Get-Mailbox | Search-Mailbox -SearchQuery "From:[email protected] AND Subject:'Voicemail Message' AND received:< $DT" -TargetMailbox "helpdesk" -TargetFolder "SearchAndDeleteLog" -logonly -loglevel Full
    

    For more information, please refer to the below link:

    How to Delete Emails Older than X days using Powershell