How to remove the SMTP address for a secondary domain from all Exchange mailboxes?

16,456

Solution 1

Here is a better version than my original answer (it handles multiple addresses to remove for each mailbox and also gives more output):

$domain = 'somedomain.com'

$mbxs = Get-Mailbox -ResultSize Unlimited

foreach($m in $mbxs)
{
    Write-Host 'Mailbox:' $m.SAMAccountName
    $addrs = $m.EmailAddresses
    for($i = 0;$i -lt $addrs.Count;$i++)
    {
        $addr = $addrs[$i]
        if($addr.PrefixString -eq 'smtp'-and $addr.SmtpAddress -like '*@' + $domain)
        {
            Write-Host 'Removing address:' $addr.SmtpAddress
            $m.EmailAddresses.RemoveAt($i)
            $i--
        }
    }
    if($addrs.Changed)
    {
        Write-Host 'Saving mailbox:' $m.SAMAccountName
        Set-Mailbox $m -EmailAddresses $addrs
    }
    else
    {
        Write-Host 'No address to remove'
    }
}

Solution 2

Test this in a lab and/or on a small set of test users before hitting production with this please.

$SMTPDomainToRemove = "@OldDomain.com"
$AllUsers = Get-ADUser -Filter * -Properties proxyAddresses
Foreach($usr In $AllUsers)
{
    $NewAddressList = @()
    $OldAddressList = $usr.proxyAddresses
    Foreach($addr In $OldAddressList)
    {
        If(!($addr -Match $SMTPDomainToRemove))
        {
            $NewAddressList += $addr
        }
    }
    If($NewAddressList.Count -GT 0)
    {
        Set-ADUser $usr -Replace @{ 'proxyAddresses' = $NewAddressList}
    }
}

The idea is that we're taking the proxyAddresses address list of each user, stripping out the ones that have the old domain name in them, and then replacing the address list with the new, updated one that does not contain references to the old domain name.

http://blogs.technet.com/b/exchange/archive/2005/01/10/350132.aspx

E-Mail Address Attributes

Exchange stores and uses information about the e-mail addresses of a recipient in the following attributes: proxyAddresses

This is the main attribute where e-mail address information is kept. When you open the properties of a recipient in Outlook and look at the "E-mail Addresses" tab, you are looking at this attribute. This is a multi-valued string containing all the addresses that represent the recipient. Each value must have the following format: type:address

For example: SMTP:[email protected]

When the type is in uppercase letters, the address is considered to be the primary address of that type and it is used as the default reply address of that recipient. When the type is in lowercase letters, the address is considered a secondary address and is used to resolve addresses during e-mail delivery, allowing the same recipient to receive e-mails directed to different e-mail addresses.

Share:
16,456

Related videos on Youtube

Massimo
Author by

Massimo

"Against stupidity, the Gods themselves fight in vain." https://www.linkedin.com/in/massimo-pascucci

Updated on September 18, 2022

Comments

  • Massimo
    Massimo over 1 year

    I have an Exchange server which used to manage multiple SMTP domains; now it only has to manage one.

    I have already removed the secondary domain from all address policies; I need to remove all SMTP addresses referencing it.

    All users have their primary SMTP address set to use the primary domain; but almost all of them have another address using the secondary domain. They also have many other different addesses: the SIP one used for lync, and one or more X500 ones derived from previous migrations. These should not be touched at all.

    How can I remove all those secondary SMTP addresses without affecting anything else?

    • joeqwerty
      joeqwerty over 10 years
      ADModify.NET - admodify.codeplex.com
    • kralyk
      kralyk over 10 years
      +1 for ADModify.NET. It will do this easily...and can even make sure the "primary SMTP address/reply to" is set if necessary.
  • Mathias R. Jessen
    Mathias R. Jessen over 10 years
    Well, my primary address is [email protected] and now I don't have to mind my inbox anymore, thanks! :D
  • Ryan Ries
    Ryan Ries over 10 years
    OK... How about $addr.ToLower().Trim().EndsWith($SMTPDomainToRemove.ToLower(‌​).Trim())... better? :)
  • Ryan Ries
    Ryan Ries over 10 years
    Or even just a well-placed @ sign... ;)