Share via

Powershell script to delete emails from mailbox older than an 7 month or specified dates

Anonymous
2020-08-04T11:27:41+00:00

Hello, I have problem with converting my Search-Mailbox command to another one. If you all know in the future as Search-Mailbox is retired. So I need to use another cmdlet for this command.

My working right now command with Search-Mailbox

Search-Mailbox -Identity ******@test.comm -SearchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))" -deletecontet

I read a lot of times https://docs.microsoft.com/en-us/exchange/policy-and-compliance/ediscovery/delete-messages?view=exchserver-2019#step-2-delete-the-message and try do step by step and get this code.

New-ComplianceSearch -Name "Remove older than 7 month messages" -ExchangeLocation ******@test.com -ContentMatchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))"

Start-ComplianceSearch -Identity "Remove older than 7 month messages"

New-ComplianceSearchAction -SearchName "Remove older than 7 month messages" -Purge -PurgeType SoftDelete

But its not work for me. getting error 

Unable to execute the task. Reason: The search "Remove older than 8 month messages" is still running or it didn't

return any results. Please wait until the search finishes or edit the query and run the search again.

my full PowerShell script

Start-Transcript

$smtpServer="smtp.office365.com" # Office 365 official smtp server 

$from = "IT Support <******@test.com>" # email from  

$logging = "Enabled" # Set to Disabled to Disable Logging 

$testing = "Disabled" # Set to Disabled to Email Users 

$testRecipient = "******@test.com"  

$date = Get-Date -format ddMMyyyy 

$Username = "******@test.com"

$Password = "test-" | ConvertTo-SecureString -AsPlainText -Force

$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri  https://eur04b.ps.compliance.protection.outlook.com/powershell-liveid?PSVersion=5.1.17763.1007 -Credential $UserCredential -Authentication Basic –AllowRedirection

 Import-PSSession $Session

Get-ComplianceSearchAction

New-ComplianceSearch -Name "Remove older than 7 month messages" -ExchangeLocation ******@test.com  -ContentMatchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))"

Start-ComplianceSearch -Identity "Remove older than 7 month messages"

New-ComplianceSearchAction -SearchName "Remove older than 7 month messages" -Purge -PurgeType SoftDelete

Microsoft 365 and Office | Subscription, account, billing | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2020-08-05T15:00:27+00:00

Hi Kepsnys,

Thank you for sharing such update with us.

Based on your mentioned reply, if you have already applied new created retention policy to your mailbox, generally it can take up to 7 days in Exchange Online for the new retention settings. But, you can use start-ManagedFolderAssistant cmdlet in Exchange Online PowerShell to immediate effect for delete message. Please refer to this detailed information article: Run the Managed Folder Assistant to apply the new settings

Please feel free to contact us, if you need any further assistant. We will further assist you. We are highly appreciated your cooperation in our forum community.

Regards,

Darpan

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-08-05T07:27:58+00:00

    with RetentionPolicy i add it. But dont delete message's maybe exist some way how to start it?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2020-08-04T14:30:46+00:00

    Hi Kepsnys,

    Thank you for your posting in the forum community. We will try to provide possible information from our end in the forum community.

    According to your mentioned description, if you want to delete email message older than 7 month from mailbox, you may try to create a retention tag and policy to delete email message from mailbox. After applying the retention policy, the next time the MRM mailbox assistant processes the mailbox, messages are moved or deleted based on settings you configured in the retention tags.

    Here is the detailed information article about create a retention tag and policy : https://docs.microsoft.com/en-us/exchange/security-and-compliance/messaging-records-management/create-a-retention-policy

    Generally, this retention policy defines how long a message remains in the mailbox and what action is taken when the message reaches the specified retention age. When a message reaches its retention age, it's either moved to the user's archive mailbox or it's deleted.

    After creating this new retention policy and applying the new policy to mailbox, it can take up to 7 days in Exchange Online for the new retention settings to be applied to the mailboxes. This process called the Managed Folder Assistant process for user's mailbox. You may also refer ManageFolderAssistant topic in this mentioned article: https://docs.microsoft.com/en-us/microsoft-365/compliance/set-up-an-archive-and-deletion-policy-for-mailboxes?view=o365-worldwide

    We hope above mentioned information will provide you  some suitable information about delete message from mailbox.

    Further, regarding your description about “Powershell script to delete emails from mailbox older than an 7 month or specified dates”, since we have dedicated support forum community, which is working and handling poweshell script related concern or issue. So, we would like to recommend you please post your PowerShell script related concern into dedicated Social TechNet forum community for further information and assistant. Our dedicated expert team will further guide you about PowerShell script related thing.

    We are truly appreciating your cooperation in our forum community.

    Regards,

    Darpan

    Was this answer helpful?

    0 comments No comments