<

Removing obsolete SMTP Domains from Exchange 2007 Mailboxes

Published on
18,256 Points
8,056 Views
2 Endorsements
Last Modified:
Awarded
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.

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
Comment
Author:GusGallows
5 Comments
 

Expert Comment

by:alt21
Hi,

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

Regards,
0
 
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.
0
 
LVL 12

Author Comment

by:GusGallows
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
0
 
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!
0
 

Expert Comment

by:einaaii
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=$_} } }
0

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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…
Please check the video also in regards to recovery of deleted emails from office 365 admin center and through the MFCMAPI tool. I have mentioned each and every step with the proper steps that need to be taken care of.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month