The intermediate files are handy, save them and set aside. If you screw up, you'll know what users to go back and fix!



get-aduser -filter * -SearchBase "OU=WHATOU,DC=DOMAIN,DC=DOMAIN" -properties * | where {$_.distinguishedname -notlike "*OUyouDONTwant*" -and $_.distinguishedname -notlike "*UnwantedOU*"} | Where {$_.emailaddress}| where {$_.emailaddress -notlike "*UnwantedSuffix*"} |Where {$_.proxyAddresses} | Where {-not($_.proxyAddresses  -like "*NewProxyAddress*")} | select-object samaccountname | Export-Csv c:\scripts\NO_NewSuffix.csv -NoTypeInformation

 

get-content c:\scripts\NO_NewSuffix.csv | select-string -pattern 'SamAccountName' -notmatch | % {$_ -replace '"', ""} | out-file c:\scripts\NO_NewSuffix_C.txt

 

$noNewSuffix = Get-Content c:\scripts\NO_NewSuffix_C.txt

 

foreach($user in $noNewSuffix)

{

$client = get-aduser "$user" -properties *

$emailP = $client.emailaddress

write-host "Email: $emailP" ##output to confirm whats happening

$newproxy = $emailP -replace '@OLD.SUFFIX','@NEW.SUFFIX'

write-host "NewProxy: $newproxy" 

##output to confirm whats happening

$NP=[string]::concat("smtp:",$newproxy)

write-host "Appended with smtp: $NP" ##output to confirm whats happening

##runfirst without set once confirmed uncomment set ## set-aduser -identity "$user" -add @{proxyaddresses="$NP"}