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 )
    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.


Posted by Kanta Prasad on
I am extending this incase needed for a long list of mailboxes :

Step 1 : Created a CSV File with the name input-change.csv

PrimarySmtpAddress, Remvoe_smtpAddress
Employee1@kantaprasad.com, oldSMTPAddress1@kantaprasad.com
Employee2@kantaprasad.com, oldSMTPAddress2@kantaprasad.com
Employee2@kantaprasad.com, oldSMTPAddress3@kantaprasad.com

Step 2: Import CSV file (Primary smtp address and RemovalSMTPAddress

$MBXs = import-csv "C:\temp\kanta\input-change.csv.csv"

Step 3 : runthe below command to remove the required SMTP addreses from list of mailboxes as mentioned in csv file:

$MBXs | foreach { Set-Mailbox -identity $_.PrimarySmtpAddress -EmailAddresses @{remove= $_.Remove_smtpAddress}}

Please provide the feed back if usefull.

Kanta Prasad
Posted by Garry on
Could this be changed from mailbox to any recipient? Such as group, user or contact?
So like "Get-Mailbox -result unlimited" changed to "Get-Recipient -result Unlimited"?
Posted by jimmy on
Hey Kanta,

how can i add more smtp address to remove? i assumed like this:

$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 "*zorchit.com", "*nicoleloftus.com",

"*guerrieriproductions.com", "*attcollection.com", "*saraleemerchandise.com", "*hitbig.com", "*hitbigsports.com")
Write-host($address.AddressString.ToString() | out-file c:\addressesRemoved.txt -append )

Set-Mailbox -Identity $_.Identity -EmailAddresses $_.EmailAddresses

but it does not work.

THanks for your reply.

Posted by AH on
Here's another way of accomplishing the same thing. Just replace with the alias you want to remove, and with the log location. If you don't need logging just remove the Write-Output line ("Removed alias...").

You can change the for multiple alias removals.


$Mailboxes = Get-Mailbox -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes){
$RemoveSmtp = "smtp:" + ($Mailbox).Alias + "@"
if (($Mailbox).EmailAddresses -contains $RemoveSmtp){
Set-Mailbox $Mailbox -EmailAddresses @{remove=$RemoveSmtp}
"Removed alias: $RemoveSmtp from " + ($Mailbox).Alias | Out-File -FilePath "" -Append
Posted by John on
How would you do the above but for a select list of mailboxes passed in a CSV file? i.e. I want to just test this against a sub set of users first.
Posted by VirgilUrivy on
You can download 7000000 Tracks, high Quality 320kbbs.
Beatport, Traxsource and FLAC 14TB. Private FTP
All Genre House, Club, Dance, Minimal, Psy-Trance, Dubstep....
Posted by Tino Schwarze on
Thanks for the starting point!

I found a simpler approach to filter the addresses in foreach pipe:

foreach {
$ea = $_.EmailAddresses
$eafiltered = $ea | where-object { -not ($_.SmtpAddress -like "*@domaintoremove.com") -or $_.IsPrimaryAddress -eq $true }
if ($ea.Count -ne $eafiltered.Count) {
Set-Mailbox -Identity $_.Identity -EmailAddresses$eafiltered
Posted by FloRod on
Script worked perfect for me.

Leave a Reply

(Your email will not be publicly displayed.)