Sometimes you need to delete an email from exchange and you usually need to do it in a hurry so Powershell is the fastest way to do this. First you need to have a connection to exchange in Powershell. For on-prem Excahnge you can RDP to the server and open the Exchange Powershell console straight from the start menu. Otherwise to get a connection you can see this post. To find and delete email we will use the following commandlets: Get-Mailbox and Search-Mailbox you can review the extra switches from those links. Here we are simply going to search by a subject and delete all the emails found in that query.
To find email
To find an email or emails you run the following command. This is searching by Subject but you can search other fields as well:
Replace USERNAME with the username of the mailbox you want to seach
Replace YOURUSERNAME with your username
Replace ‘virus’ with the subject search term you want to use. You can change Subject to any other mail field like Body.
Get-mailbox -identity USERNAME | search-mailbox –searchquery “Subject:’virus’” –Logonly –Targetmailbox YOURUSERNAME –Targetfolder Inbox
In Outlook under your inbox you will have a new folder that has the results of your query. Since we put in the -Logonly switch it will just tell you the count of how many emails were found. If you want to see the actual emails simply remove the -Logonly switch and re-run the command and in that same folder in your inbox you will get a copy of the actual emails to review.
To Delete email
Once you are happy with your find query you can delete the email by changing the command just a little bit. We remove the -TargetMailbox and -TargetFolder and -LogOnly (if you used it) and add in the switch -DeleteContent.
Get-mailbox -identity Username | search-mailbox –searchquery “Subject:’virus’” –DeleteContent