[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Removing obsolete SMTP Domains from Exchange 2007 Mailboxes

Published on
18,383 Points
2 Endorsements
Last Modified:
Community Pick

Legacy E-mail Addresses

Have you inherited an environment filled with out of date or obsolete elements or simply just had so many changes that some parts fell through the cracks? If so, you may be looking for ways to clean it up a bit. The following tackles one of those issues where you have legacy E-mail addresses remaining in your environment after a business or brand change.

Scenario: Years of change

My company has gone through many acquisitions and changes over the last several years. Throughout the process, some of the SMTP domain names in question are no longer owned by us, and as such have been removed from Exchange 2007's Accepted Domains and from the E-mail Address Policies. The problem is, removing them from the Accepted Domains and from the E-mail Address Policies does not remove their associated e-mail addresses from the users' mailboxes. This leaves us with several obsolete email addresses being associated with the mailboxes.

For instance, Company A sells off its branch with a branded domain called OLDDOMAIN.com. Company A no longer owns that brand and should therefore remove OLDDOMAIN.com from its Exchange environment entirely.

The following screenshot shows what it looks like before the script runs:
 User Screenshot before running script

What can you do?

Since there is no built-in functionality to clean off the old domain name from the users' list of E-mail Addresses (also called ProxyAddresses), we can go into each mailbox and manually remove the old address, or we can use a PowerShell script to take care of them all at once.


In the attached code example, we will make the following assumptions:

You want to run this against every mailbox in the domain.
You have Exchange Org admin rights or at least have been delegated rights to manage the mailboxes in question.
You are running this from the Exchange Management Shell.
You will replace OLDDOMAIN.com with the actual SMTP domain name you want to clean from the e-mail addresses.

The script

#Get all mailboxes into an Array
$mbxs = @(get-mailbox -Resultsize Unlimited)

#Iterate through each mailbox
foreach ($mbx in $mbxs)
	#Create two copies of the email addresses in the returned mailbox
	$CurrentList = $mbx.emailaddresses
	#the second copy is the one where the changes will take place.
	$NewList = $mbx.emailaddresses
	#Iterate through each address in the address list.
	foreach ($Address in $CurrentList) 
		#see if the current address matches the domain you are wanting to remove
		if ($Address.tostring() -match "OLDDOMAIN.com") 
			#If the match is found, remove that address from the list.
			$NewList -= $Address

	#Overwrite the old list with the new list.
	set-mailbox $mbx -emailaddresses $NewList 

Open in new window

This script worked perfectly for our environment and saved me from having to search through over 4000 mailboxes and manually change the ones I found. After running the script, I was able to confirm that the OLDDOMAIN.com SMTP address had indeed been removed.
 User Screenshot after running script

Expert Comment


This script worked great on Exchange 2007 but is not working on Exchange 2010. Just commenting so other users know.

LVL 16

Expert Comment

by:Jon Brelie
Works for me on 2010.

One question though... I cannot remove addresses if they are primary.  I've fiddled around for a while with trying to change the primary SMTP address, but it doesn't stick because $NewList contains the original Primary SMTP.  I spend a lot of time trying to Edit $NewList to reflect the address I want to change to primary, but can't get it to work.

Any thoughts on how this could be achieved?

I'm basically adding:
$tmpDomain = "@TEMP.com"
$newPrimary = ($mbx.alias + $tmpDomain)
echo "You are trying to remove a primary SMTP address"
echo "$mbx's primary address will be changed to: $newPrimary"

I've tried a million different ways to get $newPrimary into $NewList as the primary address.
LVL 12

Author Comment

Yeah, you can't leave a mail-enabled account without a primary SMTP address. What you can do is create a new Primary SMTP address and then remove the old one. To create a new Primary SMTP address, instead of trying to set a new $mbx.emailaddresses and set mailbox, you can directly set it using $mbx.primarySmtpAddress. So it would look like this:

$tmpDomain = "@TEMP.com"
$newPrimary = ($mbx.alias + $tmpDomain)
echo "You are trying to remove a primary SMTP address"
echo "$mbx's primary address will be changed to: $newPrimary"
$mbx.PrimarySmtpAddress = $newPrimary
LVL 16

Expert Comment

by:Jon Brelie
Coding with tunnel vision.  It never occurred to me to go back to the original property.  I should have asked sooner.  That works perfectly.  Thanks!

Expert Comment

easier/faster example for Exchange 2010 and newer:

$mbxs = @(get-mailbox -Resultsize Unlimited)

foreach($i in $mbxs | get-mailbox){ $i.emailaddresses | ?{$_.AddressString -like '*@OLDDOMAIN.COM'} | %{Set-Mailbox $i -EmailAddresses @{remove=$_} } }

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Join & Write a Comment

This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month