Home > Blockchain >  Powershell to replacing AD proxyAddresses with new domain
Powershell to replacing AD proxyAddresses with new domain

Time:10-20

I have smtp address beginning with nby keyword like below.

I will remove [email protected] and [email protected] and add [email protected] and [email protected]. So I will replace those with new domain.

for example :

sAMAccountName,ProxyAddresses
user01,SMTP:[email protected];smtp:[email protected];smtp:[email protected]
user02,SMTP:[email protected];smtp:[email protected];smtp:[email protected]
....
so on

Here is my script so far:

$Users = import-csv "c:\temp\users.csv"

Foreach ($User in $Users)

            {
            $Samaccountname = $User.samaccountname
            
           
            Set-ADUser $samaccountname -Remove @{proxyAddresses=$SMTP}
            Set-ADUser $samaccountname -Add @{proxyAddresses=$SMTP}
            }        

CodePudding user response:

Continuing from my comment, to replace those addresses you can do like below:

$Users = Import-Csv -Path "c:\temp\users.csv"
foreach ($user in $Users) {
    $sam = $user.samaccountname
    $adUser = Get-ADUser -Filter "SamAccountName -eq '$sam'" -Properties ProxyAddresses
    if ($adUser) {
        $proxies = $adUser.ProxyAddresses | ForEach-Object {
            $_ -replace '^(smtp:nby. )@olddomain\.com$', '[email protected]'
        } | Sort-Object -Unique

        # $proxies needs to be a strongly typed string array, so cast to [string[]]
        $adUser | Set-ADUser -Replace @{proxyAddresses = [string[]]$proxies}
    }
    else {
        Write-Warning "Could not find user '$sam'.."
    }
}
  • Related