Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

Removing obsolete SMTP Domains from Exchange 2007 Mailboxes

GusGallowsSupport Escalation Engineer
CERTIFIED EXPERT
Published:

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.

Assumptions

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
2
8,484 Views
GusGallowsSupport Escalation Engineer
CERTIFIED EXPERT

Comments (5)

Commented:
Hi,

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

Regards,
Jon BrelieSystem Architect
CERTIFIED EXPERT

Commented:
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.
GusGallowsSupport Escalation Engineer
CERTIFIED EXPERT

Author

Commented:
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
Jon BrelieSystem Architect
CERTIFIED EXPERT

Commented:
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!

Commented:
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=$_} } }

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community