Share via


Change primary SMTP in proxyaddresses AD attribute

Question

Wednesday, September 12, 2018 10:37 AM

My organisation has changed from .co.uk to .com

We sync AD to Office 365 Exchange Online.  The .com addresses are already present as a non primary address in AD (so currently smtp:[email protected]).  We need to swap the primary so that the .co.uk remains as a non primary but .com becomes the primary. 

Additionally, we need to update the UPN and mail attributes to reflect the change.  I know UPN can be changed en masse in the GUI.

This post below almost gives me what I need (once UPNs have all been updated), but removes all proxyaddresses attribute entries except for the primary.

Any help would be appreciated.

https://social.technet.microsoft.com/Forums/en-US/2f425e58-6191-4bdf-812e-dc7209b46c71/script-to-populate-proxyaddresses-and-mail-attribute-based-on-the-userprincipalname?forum=winserverpowershell 

All replies (13)

Friday, September 14, 2018 11:36 AM ✅Answered

Sorry.  This should work.

Get-ADUser -Filter * -Properties mail,ProxyAddresses |
    Foreach {  
        $proxies = $_.ProxyAddresses | 
            ForEach-Object{
                $a = $_ -replace 'SMTP','smtp'
                if($a -match 'domain to make promary'){
                    $a -replace 'smtp','SMTP'
                }else{
                    $a
                }
            }
        $_.ProxyAddresse = $proxies
        Set-ADUser -instance $_
    }

\(ツ)_/


Wednesday, September 12, 2018 11:33 AM

help set-maibox -online

Set-Mailbox <alias> -PrimarySmtpAddress [email protected]

\(ツ)_/


Wednesday, September 12, 2018 11:35 AM

Thanks for your reply.  That's not quite what I'm after as I need to make changes to the proxyaddresses attribute in our on-prem AD, rather than any changes in Exchange Online.


Wednesday, September 12, 2018 11:36 AM

What is stopping you from making those changes?  Post your script.

\(ツ)_/


Wednesday, September 12, 2018 11:38 AM

What is stopping you from making those changes?  Post your script.

\(ツ)_/

The fact I haven't got the correct script yet.  Really appreciate your input, jrv.

Assistance from anyone else would be appreciated.


Wednesday, September 12, 2018 11:39 AM

You have to get the AD user and proxyaddresses then alter the address array and use Set-AdUser to reassign the array.

\(ツ)_/


Friday, September 14, 2018 10:38 AM

So far I have this, which sets the primary SMTP address to match the UPN, which is what we need.  But we also need to leave the current SMTP in the proxyAddresses list.

Get-ADUser -Filter * -Properties mail,ProxyAddresses | Foreach {  $_.ProxyAddresses = ("SMTP:" + $_.UserPrincipalName) $_.mail = $_.UserPrincipalName Set-ADUser -instance $_}

Friday, September 14, 2018 10:53 AM

First learn to correctly format code so it is readable.  Also you code, as posted, cannot possibly work.

Here is how to reset the primary:

Get-ADUser -Filter * -Properties mail,ProxyAddresses |
    Foreach {  
        $proxies = $_.ProxyAddresses | 
            ForEach-Object{ 
                $_ -replace 'SMTP','smtp' 
            } |
            where{$_ -match 'domain to make promary'} |
            ForEach-Object{ $_ -replace 'smtp','SMTP' }
        $_.ProxyAddresse = $proxies
        Set-ADUser -instance $_
    }

If you do this to every account in the domain you will likely beak many accounts if system accounts require special addresses.  You must think of how to obtain only user accounts that are users and not system accounts.

\(ツ)_/


Friday, September 14, 2018 11:11 AM

First learn to correctly format code so it is readable.  Also you code, as posted, cannot possibly work.

Here is how to reset the primary:

Get-ADUser -Filter * -Properties mail,ProxyAddresses |
    Foreach {  
        $proxies = $_.ProxyAddresses | 
            ForEach-Object{ 
                $_ -replace 'SMTP','smtp' 
            } |
            where{$_ -match 'domain to make promary'} |
            ForEach-Object{ $_ -replace 'smtp','SMTP' }
        $_.ProxyAddresse = $proxies
        Set-ADUser -instance $_
    }

If you do this to every account in the domain you will likely beak many accounts if system accounts require special addresses.  You must think of how to obtain only user accounts that are users and not system accounts.

\(ツ)_/

Thanks for your advice and assistance.  Your script does replace the primary SMTP, but again, same as mine, doesn't leave the current primary still in place as a non-primary.


Friday, September 14, 2018 11:16 AM

Yes it does.  It keeps all addresses and then reassigns the primary.

Change "-replace" to "-creplace" which will prevent ambiguities.

\(ツ)_/


Friday, September 14, 2018 11:20 AM

Yes it does.  It keeps all addresses and then reassigns the primary.

Change "-replace" to "-creplace" which will prevent ambiguities.

\(ツ)_/

Again, thanks for your help.

I'm afraid, in my testing, it simply replaces the primary SMTP and removes all other entries, even when I use -creplace.


Friday, September 14, 2018 1:42 PM

Sorry.  This should work.

Get-ADUser -Filter * -Properties mail,ProxyAddresses |
    Foreach {  
        $proxies = $_.ProxyAddresses | 
            ForEach-Object{
                $a = $_ -replace 'SMTP','smtp'
                if($a -match 'domain to make promary'){
                    $a -replace 'smtp','SMTP'
                }else{
                    $a
                }
            }
        $_.ProxyAddresse = $proxies
        Set-ADUser -instance $_
    }

\(ツ)_/

This did the trick.  Thanks again for your help.


Wednesday, June 19, 2019 6:08 AM

How would this script be changes to process user ID's from CSV file - thus controlling which users are modified ?