Friday, June 10, 2011

Delete emails in multiple mailboxes in Exchange 2010 SP1

Export-Mailbox is no longer supported in Exchange 2010 sp1, in stead there is New-MailboxExportRequest but the problem is that “-DeleteContents” is not supported with mailbox export.

In order to be able to search and delete emails from multiple Exchange 2010 mailboxes one has to have the following roles assigned.

New-ManagementRoleAssignment -Role "Support Diagnostics" –User username

New- ManagementRoleAssignment -Role "Mailbox Import Export" -User username

above will ensure that a user has proper permissions to perform this command.

In order to find and delete a certain email from one mailbox you can perform the following command

Search-Mailbox -Identity username -SearchQuery 'subject:"Email Subject that you want to search for"' –DeleteContent

this can be run for all mailboxes on a server as well

Get-Mailbox –Server “*servername*” –ResultSize unlimited | Search-Mailbox -SearchQuery 'subject:"Email Subject that you want to search for"' –DeleteContent


  1. For working out troubles related to corrupted or lost edb files you may use EDB recover, which allowing to work under all Windows OS and with all versions of MS Exchange Server. It uses all modern methods of restoring data.

  2. When I attempt the Get-Mailbox –Server “*servername*” –ResultSize unlimited | Search-Mailbox -SearchQuery 'subject:"Email Subject that you want to search for"' –DeleteContent command, all I get is a >> prompt.

    1. you are missing a ' at the end.

  3. Try server name withou ""
    The prompt means that your command is not completed

  4. Thanks, got a rogue mail fixed with this and another resource.

  5. This is great, been looking for it. However, i am having problem with running shell cammand. Following is result, HELP PLEASE
    The term 'Search-Mailbox' is not recognized as the name of a cmdlet, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:69
    * Get-Mailbox -Server "****" -ResultSize unlimited | Search-Mailbox <<<< -SearchQuery 'Subject:"test email"' -DeleteContent
    +CategoryInfo : ObjectNotFound: [], CommandNotFoundException
    = FullyQualifiedErrorId : CommandNotFoundException

  6. I recommend that first you run the command without -DeleteContents. Add -LogLevel Full -TargetMailbox "My Name" -TargetFolder "a mailbox folder name for this"

    When done, it will put an email in your target folder with a CSV attachment with the results. You can then make sure you are only deleting what you want.

    Also add other parameters to the searchQuery to make sure you get it right: -SearchQuery 'subject:"the bad email subject" sent:today from:"name of person who goofed"'

    Once you verify that you will only be deleting the emails you expect, run the same command and add the -DeleteContents switch. The resulting csv in your mailbox is the log record of what happened.

    1. Dear Jeffrey,

      Thank you for a very valid suggestion.

  7. Thanks Bhojraj,this helped us get rid of an email a naughty student sent around school. We had to enable the command first, I referenced for anyone who has the same issue


Leave a comment if you find this post useful or if you have any questions