#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
}
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.
Comments (5)
Commented:
This script worked great on Exchange 2007 but is not working on Exchange 2010. Just commenting so other users know.
Regards,
Commented:
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.
Author
Commented:$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
Commented:
Commented:
$mbxs = @(get-mailbox -Resultsize Unlimited)
foreach($i in $mbxs | get-mailbox){ $i.emailaddresses | ?{$_.AddressString -like '*@OLDDOMAIN.COM'} | %{Set-Mailbox $i -EmailAddresses @{remove=$_} } }