Loading Now

Use Powershell to find and delete email from Exchange

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

My name is Skylar Pearce, I have been working as a System Administror since 2013 as well some side consulting work. During my career I have worked with everything from Active Directory and vCenter to configuring routers and switches and phone systems, documenting and scripting my way through the whole thing. I have a Security+ certification and am currently working on my PenTest+. Throughout my career I have gained almost all of my knowledge from blogs like this. It is now time for me to pay it back. Over time I have gathered scripts and tricks over the years that I will share on this site. A lot of the posts here will be mainly reference posts, some will be full on how to’s. I am happy to go into more depth on any other topics I go over here, just make a comment on a post. I will do my best to post once a day on weekdays but as I run out of ideas it may slow down. My WordPress skills are still growing so the site will likely get better over time as I learn. You can reach me at contact@allthesystems.com or on LinkedIn