Master Exchange 2007

powershell, automation & more…

  • Categories

  • Archives

Export-Mailbox….for deleting content from mailboxes

Posted by shauncroucher on August 10, 2009

OK, this is a pretty versatile cmdlet.

Export-Mailbox is a great tool for exporting mailbox content to another Mailbox or PST file, but it is also a great tool to use for removing messages from Mailboxes, either on a per server, per mailbox database, per storage group or just individually.

There is a whole plethora of options available to filter the mailboxes and content that is included. Perhaps an external user (or internal if you are having problems with employees!) has sent an email that is offensive, includes sensitive information or is just not a message that should be seen by the addressed recipients. Export-Mailbox may be the command to use to remove this from your organisation.

For example, the following will delete all messages from all mailboxes where the subject is “Shouldnt be reading this”

Get-Mailbox | Export-Mailbox -SubjectKeywords “Shouldnt be reading this” -Deletecontent -confirm:$false -whatif (omit -whatif to actually run the command).

So, what this is doing is piping all the mailboxes in the entire organisation to the Export-Mailbox command. We have omitted parameters such as -TargetMailbox and -TargetFolder because we do not wish to see the results of the Export-Mailbox command, we simply wish to remove the messages, which we are doing by adding the parameter -DeleteContent.

So, let’s say we are interested to see how many recipients have received this message. Simply add the parameters:

Get-Mailbox | Export-Mailbox -SubjectKeywords “Shouldnt be reading this” -Deletecontent -TargetMailbox <Mailbox you want to use to hold the recoverred items> -TargetFolder <A folder in which to store the recovered data in the targetmailbox> -confirm:$false -whatif (omit -whatif to actually run the command).

There are many more parameters for the command (please see Technet link below for full listing) such as -ContentKeywords to scan the message body and attachment for a keyword, and -AllContentKeywords which will scan the body, subject, attachment and content of the message.

If you have many mailboxes you wish to run this command on there are two things you can do to expidite the completion of this task.

1) Include only folders of interest to scan for the content. Use the -IncludeFolders for this.
2) Use the -maxthreads parameter to increase the amount of mailboxes to scan through simultaneously.

One common complaint when using this tool is that the process returns MAPI failures, or errors relating to the creation of the TargetFolder. The account you use to run this kind of task will require FullAccess on ALL the mailboxes. Use the Add-MailboxPermissions to grant this right, or perform this at the MailboxDatabase level. Just make sure you have rights!

It should also be noted that as long as you are running Exchange 2007 SP2, the ‘keyword’ command will catch all mail items that contain the keyword ANYWHERE in the field, regardless of spacing. So for example, if you are using -SubjectKeywords “Viagra” this will be a hit for “Viagra Cheap”, “Get yourViagraHere” and ”     **  Viagra  **”.

There were reported problems surrounding the logic of the filter command before SP2, so if you find the export command is not behaving like this for you, the best thing to do initially is upgrade to at least Exchange SP2.

Refer to the following for more information on this useful cmdlet: (full listing of Export-Mailbox parameters),-hello-export-mailbox/



3 Responses to “Export-Mailbox….for deleting content from mailboxes”

  1. Mike Klein said

    Any idea of how to delete Mailbox folders from users.

    For example: If all my users had a folder called “Stuff” under there inbox. How can I delete them from powershell for all users?

  2. Yasir said

    Hi Shaun,
    I am looking for the power shell command or any script to delete a specific attachment from everyone’s email.

    I am able to search and move/delete the attachment available mail from all DBs & mailboxes using the power shell command below. But i need to delete the attachments only without deleting the email from the mailbox.

    Get-mailbox -database ‘DB1’ | export-mailbox –AttachmentFilenames “movie” -TargetFolder “MovieAttachmentData” -TargetMailbox Administrator –DeleteContent


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: