Tuesday, June 4, 2013

How to Delete Mail from Multiple Mailboxes

How to Delete Mail from Multiple Mailboxes
 
Issue:

I had this recently where a spam email had went around.  I needed to find a way to delete the email from every mailbox. This can be done from Powershell command:
 
NOTICE out of the box these commands are not enabled, you will need to see this article first if you have not already enabled them – Enable Export Mailbox Commands In Exchange 2010
 
 
Once the above is done you can now proceed the following powershell cmdlet:
 
get-mailbox -result unlimited -Server YourServername | Search-Mailbox -SearchQuery 'Subject:"Your XYZ order"', 'from:"mailto:sender@xyz.com%22', 'body:"your body text"' -TargetMailbox hltest -TargetFolder "xyzspam"  -LogLevel full -DeleteContent >C:\deletedxyz10.txt
 
Where  YourServername is your mailbox server and "Your XYZ order" is the subject content, from, and body, you can search for one of them, or combination of two or three
 
You will need to use an account that has the correct permissions to give the correct permissions to the user use this command
get-mailbox -server "yourservername"| add-mailboxpermission -user administrator -AccessRights FullAccess
That command will give the correct rights to the administrator account.
 
With above commands you will be able to remove email from multiple mailboxes in Exchange 2010 and Exchange 2013.

How to Enable Export-Mailbox or Import-Mailbox Powershell commands

When using powershell in  Exchange 2010 or Exchange 2013 and you try Export-Mailbox or Import-Mailbox the commands or delete messages from multiple mailbox, you will get error since these are not available by default, we need to enable them…
 
Note, if you are running exchange 2010 SP1 and above and you are trying to delete multiple emails then go here http://www.techieshelp.com/delete-mail-from-multiple-mailboxes/
IT Solution
1) Check if you have the Exchange Server Admin Tasks installed in the Exchange Management Shell
 

Get-PSSnapin
 
2) It should not be in the list displayed as you cannot run the command. Run the command below to see what is registered.
 

Get-PSSnapin -registered
 
3) Now to install the appropriate "Admin Tasks for the exchange Server" run the command below:
 

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
 
NOTE: for MS Exchange 2007 you should change the point (3) to a command like the one below:
 

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
 
You will now find the import and export commands are available in powershell
If you want to add multiple users or groups to be able to run these commands use the command below, it will create two new security groups with the members you specify.
New-RoleGroup -Name "Exchange Mailbox Import Export" -Roles "Mailbox Import Export" -Members "<yourdomainyourgroupname>" -DisplayName ""Exchange Mailbox Import Export" -Description "This group handles permissions to the import and export commands in powershell."
That creates a group and adds users who are allowed access to the import and export powershell commands.
New-RoleGroup -Name "Exchange Support and Diagnostics" -Roles "Support Diagnostics" -Members "" -DisplayName ""Exchange Support Diagnostics" -Description "This group handles permissions to the support diagnostics cmdlets in exchange."."
That creates a group and adds users who are allowed access to all the exchange powershell diagnostics applets