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 | 
            ForEach-Object{
                $a = $_ -replace 'SMTP','smtp'
                if($a -match 'newdomain.com.au'){
                    $a -replace 'smtp','SMTP'
                }else{
                    $a
                }
            }
        $_.ProxyAddresse = $proxies
        Set-ADUser -instance $_

    }

13 comments:

  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."
    $count++
    }
    else
    {

    Write-Host "Nothing Changed."
    }


    }

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

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

      Delete
  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.

    ReplyDelete
  3. 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.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. The principal Simple Mail Transfer Protocol (SMTP) address of an account to be used for server-to-server authorisation or delegation access is represented by the PrimarySmtpAddress element.https://webdesignslash.com.au/video-animation-services/

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Cmolds stands among the top app design companies, crafting innovative and tailored solutions that redefine user experiences and drive business growth. Elevate your app vision with Cmolds' expert design expertise and create unforgettable digital journeys.

    ReplyDelete