Empty a mailbox using Exchange Web Services

Currently I’m working on an environment where 6,000 test mailboxes are created. During test migrations all kinds of information is stored in these mailboxes. You can use Exchange Web Services to empty these mailboxes.

To empty the (test) mailboxes you need the following:

  • Exchange Web Services Managed API;
  • An account with enough permissions to empty the mailboxes;
  • A script that does the actual plumbing.

The Managed API can be downloaded from the Microsoft website: http://www.microsoft.com/download/en/details.aspx?id=13480 and it runs on Windows 7 clients or Windows 2008 (R2) servers.

The script will logon to the mailboxes with an account that needs sufficient permissions. You can set the permissions on the Exchange CAS Server using the following commands:

Get-Mailbox|Add-MailboxPermission –User <serviceAccount> -AccessRights FullAccess

Get-MailboxDatabase | Add-ADPermission –User <serviceaccount> -AccessRights GenericAll ExtendedRights Send-As,Receive-As

The script itself is fairly simple:

  • It reads a text file containing all mailboxes that need to be emptied;
  • It references the Web services on the Exchange CAS Server, i.e. https://webmail.uclabs.nl/ews/exchange.asmx;
  • It references the Web services DLL on the workstation in C:\Program Files\…;
  • It asks for the credentials of the account (that was granted appropriate permissions earlier);
  • It enumerates all mailboxes in the text file, logs on to each mailbox and uses the delete function in the API to delete the messages.

A couple of remarks:

  • In the script, the actual delete is set to HardDelete, which means the message is permanently deleted. It can also be set to SoftDelete, in this case the message is moved to the dumpster;
  • The script point to the Inbox, but it can point to all well known folders in the mailbox;
  • This version is used on Exchange2010_SP1, but it can also set to Exchange2010 or Exchange2007_SP1 if you use another Exchange version.

This is the actual script:

$Mailboxlist = “c:\scripts\mailboxes.txt”
$uri = [system.URI]"https://webmail2.dotnetwork2.co.za/ews/exchange.asmx"
$dllpath = “C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll”
[void][Reflection.Assembly]::LoadFile($dllpath)
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[System.Net.NetworkCredential]$cred= Get-Credential
$deletemode = [Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService ([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
$service.Url = $uri
$service.Credentials = $cred
foreach ($MailboxName in Get-Content $Mailboxlist)
{
  write-host "Processing: $($MailboxName)"
  $rfRootFolderID = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
  $rfRootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$rfRootFolderID)
  $rfRootFolder.Empty($deletemode,$null)
}

Special thanks to Roland Verhoeven who wrote this script when we were doing a large Groupwise to Exchange 2010 migration together.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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