Rexxer

Some tips for me and other

Exchange 2010 + Powershell + Search and delete specific mail

Task: Find and delete all the e-mail that contains specific attachment.

1. Estimate and debug our query:

Get-Mailbox -ResultSize Unlimited | Search-Mailbox -SearchQuery ‘attachment:”My file example*”‘ -TargetMailbox “my.mailbox” -TargetFolder “MyFolder” -Logonly -Loglevel Full | Select DisplayName, ResultItemsCount | where-object {$_.ResultItemsCount -ne “0”}

and check the folder for the report or

Get-Mailbox -ResultSize Unlimited | Search-Mailbox -SearchQuery ‘attachment:”My file example*”‘ -TargetMailbox “my.mailbox” -TargetFolder “MyFolder” -EstimateResultOnly | Select DisplayName, ResultItemsCount | where-object {$_.ResultItemsCount -ne “0”}

2. Run and delete if everything is OK:

Get-Mailbox -ResultSize Unlimited | Search-Mailbox -SearchQuery ‘attachment:”My file example*”‘ -TargetMailbox “my.mailbox” -TargetFolder “MyFolder” -DeleteContent  -Confirm:$false -Force | Select DisplayName, ResultItemsCount | where-object {$_.ResultItemsCount -ne “0”}

Links:

Delete email from Microsoft Exchange user mailboxes using PowerShell

https://docs.microsoft.com/en-us/powershell/module/exchange/mailboxes/search-mailbox?view=exchange-ps

https://docs.microsoft.com/en-gb/exchange/security-and-compliance/in-place-ediscovery/message-properties-and-search-operators#search-tips-and-tricks

https://docs.microsoft.com/en-us/office365/securitycompliance/search-for-and-delete-messagesadmin-help

 

Comments are currently closed.