Email is at the heart of business communications and so, it shouldn’t come as a surprise when you’re called upon to perform a variety of tasks above and beyond simply keeping messages flowing.
For example, removing emails which, for whatever reason, are considered targets for deletion.
Using PowerShell, it’s possible (on both Office 365 and on-premise Exchange) to remove messages by using a modified application of the search-mailbox cmdlet.
So, to find a message within a user’s mailbox by subject heading, you can issue the following:
Get-Mailbox -Identity firstname.lastname@example.org | Search-Mailbox -SearchQuery ‘Subject:”your subject”‘ -EstimateResultOnly
And to remove the message found via your search you simply add the -DeleteContent switch:
Get-Mailbox -Identity email@example.com | Search-Mailbox -SearchQuery ‘Subject:”your subject”‘ -DeleteContent
Of course, your search parameters aren’t limited to subject line alone but can include date, keywords within a message, target folder and more.