Goal: Remove any secondary/alternate SMTP addresses for an entire domain

Difficulty: Easy

Prerequisites: Administrative access to mailboxes

In Microsoft Exchange it is easy enough to mistakenly add an smtp alias to every existing account when you add another domain to your Exchange Server. Fortunately, it's easy enough to get rid of all these unwanted secondary aliases for that domain and not remove any mailboxes that have that domain as the primary account. Simply copy the sample below, change the domain you want to remove, and run the file from a powershell console. When the script finishes you can navigate to c:\addressesRemoved.txt to get a list of all of the addresses that were removed in case you need some form of auditing.

If you want to do a dry run you can remove lines 9 and 10 below and run the script and check the file to see what is going to be removed.

$Mailboxes = Get-Mailbox -result unlimited
$Mailboxes | foreach{
    for ($i=0;$i -lt $_.EmailAddresses.Count; $i++)
    {
        $address = $_.EmailAddresses[$i]
        if ($address.IsPrimaryAddress -eq $false -and $address.SmtpAddress -like "*domainToRemove.com" )
        {
            Write-host($address.AddressString.ToString() | out-file c:\addressesRemoved.txt -append )
            $_.EmailAddresses.RemoveAt($i)
            $i--
        }
    }
    Set-Mailbox -Identity $_.Identity -EmailAddresses $_.EmailAddresses
}

 

Special thanks to Jon Brelie for reporting a bug in the script and an appropriate fix. Script was updated 5/7/2014 to reflect this patch.

Comments

Leave a Reply



(Your email will not be publicly displayed.)



Search