Exchange 2010 Powershell command to delete all content in mailbox

**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

 

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

Mark as helpful.

Posted in: Exchange, Server