Powershell: Removing Secondary SMTP Addresses for Domain

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

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
}
Loading Conversation