Pages

Wednesday, June 6, 2012

Exchange 2010 SP1 or SP2 PowerShell cmdlet for deleting all email in a mailbox

I haven’t had to do a whole lot of Exchange 2010 work recently as most of the projects I’ve been involved in were either desktop virtualization or datacenter related but just about a week ago I was asked what the cmdlet was to delete all the mail in an inbox.  I remember executing the cmdlet a while ago and after a bit of digging in my notes, I found it.  Knowing that I’m bound to come across this again, I thought I’d write a short blog on the prerequisites required for setting up the account and finally the cmdlet for future reference.

Prerequisites

The first thing you’ll need to do is ensure that you have given the account you’re going to use the proper permissions by adding them into the following groups:

  1. Discovery Management <—Already Exists
  2. Exchange Support Diagnostics <—Does not exist so you’ll need to create it
  3. Exchange Mailbox Import Export<—Does not exist so you’ll need to create it

Discovery Management

The Discovery Management group is located in the Microsoft Exchange Security Groups OU in your domain:

image

Members of this management role group can perform searches of mailboxes in the Exchange organization for data that meets specific criteria.

clip_image001

Exchange Support Diagnostics & Exchange Mailbox Import Export

The Exchange Support Diagnostics & Exchange Mailbox Import Export groups are not created by default which means you’ll need to manually create them.  The following TechNet blog does a great job of explaining these groups so I won’t reiterate it here:

http://blogs.technet.com/b/exchange/archive/2010/03/26/3409621.aspx

The instructions in the blog above never worked for me as switch:

-Members "<domain\groupname>"

Would cause the cmdlet I execute to continuously fail.  My workaround is to simply omit that switch and add the members with the Active Directory Users and Computers GUI once the group has been created so to create the Exchange Mailbox Import Export group, execute the following cmdlet:

[PS] C:\Windows\system32>New-RoleGroup -Name "Exchange Mailbox Import Export" -Roles "Mailbox Import Export" -DisplayName "Exchange Mailbox Import Export" -Description "This group will provide access to mailbox import and export cmdlets within entire Exchange Organization."

Name                          AssignedRoles                 RoleAssignments               ManagedBy

----                          -------------                 ---------------               ---------

Exchange Mailbox Import Ex... {Mailbox Import Export}       {Mailbox Import Export-Exc... {domain.internal/Microsoft ...

[PS] C:\Windows\system32>

image

To create the Exchange Support Diagnostics group, execute the following cmdlet:

[PS] C:\Windows\system32>New-RoleGroup -Name "Exchange Support Diagnostics" -Roles "Support Diagnostics" -DisplayName "Exchange Support Diagnostics" -Description "This group will provide access to support diagnostics cmdlets within entire Exchange Organization."

Name                          AssignedRoles                 RoleAssignments               ManagedBy

----                          -------------                 ---------------               ---------

Exchange Support Diagnostics  {Support Diagnostics}         {Support Diagnostics-Excha... {domain.internal/Microsoft ...

[PS] C:\Windows\system32>

image

Once those 2 cmdlets have been successfully executed, you should now see the following 2 groups:

image

Proceed with adding the the account you intend on executing the cmdlet to delete the contents in a mailbox into those 2 groups.

Deleting contents of a mailbox

To delete the contents of a mailbox, first obtain the identity of the mailbox by executing the following:

[PS] C:\Windows\system32>Get-Mailbox | where-object {$_.alias -match "tluk"}

Name                      Alias                ServerName       ProhibitSendQuota

----                      -----                ----------       -----------------

Luk, Terence              tluk                 svrexmb12        unlimited

clip_image001[4]

Now that you have the identity execute the following cmdlet to delete the content:

**Note that this command deletes all of the mail in the inbox as well as the folders.

[PS] C:\Windows\system32>Search-Mailbox -Identity "Luk, Terence" -DeleteContent

Confirm

Deleting content from mailboxes Luk, Terence

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "Y"): Y

RunspaceId       : e9269d66-4888-4dd4-96cd-c72e0358e099

Identity         : domain.internal/Domain/Users/CCS/Luk, Terence

TargetMailbox    :

TargetPSTFile    :

Success          : True

TargetFolder     :

ResultItemsCount : 145

ResultItemsSize  : 18.22 MB (19,107,056 bytes)

[PS] C:\Windows\system32>

image

Check the mailbox to ensure that the content has been deleted:

image

12 comments:

Terry Lau said...

Thanks for sharing.
It is useful for me.

Anonymous said...

Hello Terry,
your post is very usefull and, first of all, I'm very thankful.
Anyway I strongly suggest to invest your time improving a little the article formatting.. It's really really unredable :P.

Anonymous said...

Thankyou! Helped me to do my job.

Unknown said...

Thanks a lot on this..It helps on my study..



Cloud archiving

Anonymous said...

i have got an error when i ran the about command

The object was bound to a wrong object type. ObjectClass = IPM.Appointment, Intended Type = Microso
ft.Exchange.Data.Storage.MessageItem.
+ CategoryInfo : ReadError: (0:Int32) [Search-Mailbox], WrongObjectTypeException
+ FullyQualifiedErrorId : E3614194,Microsoft.Exchange.Management.Tasks.SearchMailbox

Anonymous said...

Very useful. No issues at all for me. Thanks for sharing.

Spiro R. said...
This comment has been removed by the author.
Anonymous said...

Unfortnately this doesn't delete all content, for example folders that have been created. Is there a way other than changing the mailboxguid to delete all content from a mailbox and start from scratch?

Anonymous said...

Thank you. Very Helpful
regarding this part:
"The instructions in the blog above never worked for me as switch":

-Members ""

Just do it like this "sfx\domainadmin"

this worked like a charm

Unknown said...

I have found what I was searching for, in this post, useful single line command to clean up single mailbox.

Anonymous said...

Yes, thank you for this helpful post.

Regards from France :-)

Unknown said...

Hi -

This cmdlet works well, however it does NOT delete "Folders" and "Subfolders. Only the content that is contained inside of said folders.