Search for and delete content within a mailbox using PowerShell
I was recently asked to delete a specific message from a user mailbox as the recall function did not work (the message had already been read). I was able to check if the message existed in the mailbox using the Search-Mailbox cmdlet:
Search-Mailbox -Identity "Jane Smith" -SearchQuery from:"john.smith@somedomain.com",received:"19/11/2016" -EstimateResultOnly
Once I am satisfied that I have the right message I can remove it by using the DeleteContent paremeter:
Search-Mailbox -Identity "Jane Smith" -SearchQuery from:"john.smith@somedomain.com",received:"19/11/2016" -DeleteContent
As an alternative to just deleting the content straight away it can be moved to another mailbox by specifying the TargetMailbox parameter:
Search-Mailbox -Identity "Jane Smith" -SearchQuery from:"john.smith@somedomain.com",received:"19/11/2016" -TargetMailbox "Discovery Search Mailbox"
Note: The SearchQuery parameter specifies a search string using Keyword Query Language (KQL).