27 December 2018

Replace Primary SMTP Address | AD

I've recently had to replace the Primary SMTP address for all users within an organisation.  Of course this is a lot easier if you script it.

I needed to do this as the client was changing their primary address, but wanted to retain all the current proxyAddresses as well.

This was achieved by using two scripts.

$Users = Get-ADUser -SearchBase 'OU=ouname,OU=ouname,OU=ouname,OU=ouname,DC=domai,DC=com,DC=au' -Filter * -Properties mail,ProxyAddresses | Select-Object samaccountname,ProxyAddresses
Foreach ($User in $Users)

            $Samaccountname = $User.samaccountname
            $SMTP = "SMTP:$samaccountname@newdomain.com.au"
            Set-ADUser $samaccountname -Add @{proxyAddresses=$SMTP}

This script adds the new SMTP address for all users within a certain OU.  Of course this will cause some troubles on its own, as there's now going to be two SMTP addresses, marked as primary.

The following script clears this up:

Get-ADUser -SearchBase 'OU=ouname,OU=ouname,OU=ouname,OU=ouname,DC=domai,DC=com,DC=au' -Filter * -Properties mail,ProxyAddresses |
    Foreach {  
        $proxies = $_.ProxyAddresses | 
                $a = $_ -replace 'SMTP','smtp'
                if($a -match 'newdomain.com.au'){
                    $a -replace 'smtp','SMTP'
        $_.ProxyAddresse = $proxies
        Set-ADUser -instance $_



  1. This will remove smtp:firstname.lastname@domain.com and replace it with SMTP:firstname.lastname@domain.com

    [string]$proxydomain = "@Domain.com.au"; #Proxy domain
    [string]$usersOU = "OU=Users,DC=Domain,DC=local"; #OU to apply changes
    [int]$count = 0 ;

    Import-Module ActiveDirectory

    Get-ADUser -Filter "*" -SearchScope Subtree -SearchBase "$usersOU" -Properties proxyAddresses, employeeType, givenName, Surname | foreach-object {

    Write-Host "Editing user: $_.SamAccountName"

    if ($_.Proxyaddresses -match $_.givenName+"."+$_.Surname+$proxydomain)
    Set-ADUser -Identity $_.SamAccountName -remove @{proxyAddresses="smtp:"+$_.givenName+"."+$_.Surname+$proxydomain}
    Write-Host "Removed '$_.SamAccountName' smtp Address"
    Set-ADUser -Identity $_.SamAccountName -add @{proxyAddresses="SMTP:"+$_.givenName+"."+$_.Surname+$proxydomain}
    Write-Host "Result: Added '$_.SamAccountName' Proxy address."

    Write-Host "Nothing Changed."


    Write-Host "Sucessfully Edited" $count "users"

    1. Great, that has done it for me!
      Thank you

  2. This is good step because many new users were facing issues during replacing their script, but now they are happy because with the help of this content, they can solve their issues easily. Coursework writing services.

  3. QuickPay Portal | www.quickpayportal.com | Pay Your Medical Bill

    Pay Your Medical Bill using www.quickpayportal.com and Make a quick and fast payment online using the QuickPay Portal. Just find your QuickPay Code on your billing statement and and you're ready to go




  4. You have shared a very nice post but to be honest, I do not have enough time to complete it because I have to find a Research Article Writer for Publication of my article. Although, I would feel great to complete it first, but I have to find a writer on a very urgent basis.

  5. Slot Pulsa Pragmatic Play banyak sekali yang menyediakan berbagai permainan mesin slot gacor online uang asli terlengkap.

  6. banyak sekali yang menyediakan berbagai permainan mesin slot gacor online uang asli terlengkap.

  7. It can also be a component transfusion to slenderize into more detail about the us and electrolyte of a significant in proffer to accept which available laryngeal effects are close by, and how they can other you. Nigczv zrkqee

  8. Quick Pay Portal
    The team created the quickpayportal.com website to give you the option to pay your medical bills right away. Located in Massachusetts, the QuickPay Portal Bill Pay is set up by Athena physicians .
    Visit:- QuickPay Portal Com

  9. Perkenalkan REPUBLIKSLOT yakni situs slot online 24 jam terbaik 2022 yang menyediakan ribuan mesin slot pulsa gacor mudah menang jackpot terbesar dan judi fafaslot 88 terpercaya di Indonesia melalui link slot pulsa xl alternatif resmi PASTI BAYAR. Nikmati setiap kemudahan bermain slot online24jam tanpa potongan minimal 10rb dan bonus new member 100% di awal to kecil segera dan bonus besar lainnya tanpa diundi. alternatif link kunjungi https://conecta.bio/slotxl

  10. This comment has been removed by the author.