Exchange powershell Script

Hi

I can’t remove SMTP address from Exchange server via a script.
I want to change primary SMTP address and remove the old one. jd@example.com to janedoe@example.com.
I can add and change primary SMTP address but I can’t remove old one. If anyone emails jd@example.com it should bounce.

Set-Mailbox $mbox -PrimarySmtpAddress $NewSMTP -EmailAddressPolicyEnabled $false

This does not work:
Set-Mailbox $mbox -EmailAddresses @{Remove=$OldSMTP}

It works if i run it in exchange management console.

Any ideas?

Why shouldn’t it work in a script? Do you get errors? If yes - show it.

What error are you getting? If I read it right, the first command is changing the primary address, so the old one doesn’t need to be removed.

In Exchange, the proxyAddresses(the AD attribute that is presented by the EmailAddresses) are meant to keep track of the addresses associated to a user in order to maintain consistency in delivery (so if I know you were working at contoso.com, and it get acquired by tailspintoy.com, causing your primary smtp address to switch to the latter, then I can still pretend to reply to old email you sent me where contoso.com was the source address).

Said so, the EmailAddresses filed is returned as an array, and you can’t subtract from an Array object.
something like this should work:
$mbx = Get-Mailbox user@contoso.com
$addresses = $mbx.EmailAddresses | where{$_ -ne “smtp:oldAddress@contuso.com”}
$addresses
#Set-Mailbox user@contoso.com -EmailAddresses $addresses

Just uncomment the last Set- if the content of $addresses is what you expect it to be

Yes I get errors when running Set-Mailbox $mbox -EmailAddresses @{Remove=$OldSMTP} via a script connecting to Exchange powershell, but not if I run it in Exchange PS console. I’ll have to get back with errorcode.
Everything else works, everyone gets a new primary SMTP address. It’s just the old one that needs to be removed, the decision has been made.

I think you’re on to something fpoli :slight_smile:

That will most likly do what I want. I’ll test it soon.
As it is now, $mbx.EmailAddresses will contain, SIP, SMTP, x400 addresses that I don’t want removed.
The old SMTP addresses just needs to be removed,
And I really don’t want to change 150+ addresses by hand.

I really appreciate every answer. It’s amazing, thank you all.