Add proxyAddresses Attribute in AD for 3 possible mail domains

I’m new to PowerShell and I’m trying to add three proxyAddresses to each of my AD users. There are 3 domains that are used for email and each user uses one of those 3 domains as their SMTP: address and the other two need to be smtp: addresses. I was able to populate all of my AD users with our legacy email domains (with another script that someone helped me with), but I’m having trouble with having a script recognize the user’s existing SMTP: address as “firstname.lastname@domain1.com” and adding the other two as smtp:firstname.lastname@domain2.com. If everyone used the same SMTP: domain I could do it, but having to look at the user’s current SMTP:, not touching it, and adding the other two domains as smtp: is really confusing me.

Could someone help me with a script for this, please?

Hi

How about you take proxyAddresses with -clike ‘SMTP:*’ to string and then split it so you got the firstname.lastname. Then you could use that as base and add those domains to end of it and add prefix smtp: and set it with set-aduser -add @{proxyaddresses=$secondDomain,$thirdDomain}?

Jake

I’m not sure I follow, but maybe I can make this complicated. I could also use a script that just looks for SMTP:firstname.lastname@domain1.com and if it exists in a user’s proxyAddresses, add smtp:firstname.lastname@domain2.com, smtp:firstname.lastname@domain3.com. Then I could just change the values in the script so that it looks for SMTP:firstname.lastname@domain2.com…and if this exists, then adds smtp:...@domain1.com, smtp:…domain3.com. I’m just not strong enough in PowerShell yet to code that…and I’m not finding anything online yet.

Hi

No problem. If I was creating this I would use If or Switch to determ added domains dependind on what domain is primary, then crosscheck that if this mail address is already there.

Edit. Here’s non tested example to point you to direction, not the prettiest but… To be faster Switch would be better.

$Domain1 = '@domain1.com'
$Domain2 = '@domain2.com'
$Domain3 = '@domain3.com'

$PrimarySMTP = Get-ADUser $user -Properties proxyAddresses | select -ExpandProperty proxyAddresses | where {$_ -clike "SMTP:*"}
$userPrefix = $($PrimarySMTP.split('@')[0]).ToLower()


If ($PrimarySMTP -like "*$Domain1") { Set-ADUser -Identity $user -add @{proxyAddresses="$userPrefix$Domain2","$userPrefix$Domain3" }
If ($PrimarySMTP -like "*$Domain2") { Set-ADUser -Identity $user -add @{proxyAddresses="$userPrefix$Domain1","$userPrefix$Domain3" }
If ($PrimarySMTP -like "*$Domain3") { Set-ADUser -Identity $user -add @{proxyAddresses="$userPrefix$Domain1","$userPrefix$Domain2" }

Jake

A minor change and also very untested. Check to make sure those secondary addresses aren’t already in there. Even with this change, you run the risk of adding a duplicate where someone else has a similar name and the names were not duplicated consistently.

$Domain1 = '@domain1.com'
$Domain2 = '@domain2.com'
$Domain3 = '@domain3.com'

$Proxy = Get-ADUser $user -Properties proxyAddresses | select -ExpandProperty proxyAddresses
$PrimarySMTP = $Proxy | where {$_ -clike "SMTP:*"}
$userPrefix = $($PrimarySMTP.split('@')[0]).ToLower()

$Secondary1 = "$userPrefix$Domain1"
$Secondary2 = "$userPrefix$Domain2"
$Secondary3 = "$userPrefix$Domain3"

if ($Proxy -notcontains $Secondary1) {Set-ADUser -Identity $user -add @{proxyAddresses=$Secondary1}
if ($Proxy -notcontains $Secondary2) {Set-ADUser -Identity $user -add @{proxyAddresses=$Secondary2}
if ($Proxy -notcontains $Secondary3) {Set-ADUser -Identity $user -add @{proxyAddresses=$Secondary3}

Instead of combining all of my tasks into one script I have broken what I need down into 3 lines of code to help mitigate any duplicate smtp entries.

I am 99% there, I’m just having trouble getting the script to add givenname.sn@domain.com to my AD account. I have changed the code around and all I get is “smtp:.domain1.com” and “smtp:.domain2.com” added to my proxyAddresses.

$Domains = "domain1.com","domain2.com"

$proxies = foreach ($domain in $domains) {"smtp:$($_.givenname).$($_.sn)@$domain"}

Get-ADuser user1 -properties mail | Set-ADuser -Add @{Proxyaddress = $Proxies}

How should that line be written to pull the givenname and sn from an AD account and add a proxyaddress to the account as givenname.sn@domain1.com and …domain2.com?

Thanks for all of your help so far!

Hi

Quick and dirty

$ADUser = Get-ADUser -Identity testuser1
$GN = $ADUser.GivenName
$SN = $ADUser.Surname

$mail = "smtp:$GN.$SN@domain1.com"

Jake

I couldn’t get that to work. The code runs without error, but it doesn’t add anything to my AD account…

$Domains = "domain1.com","domain2.com"

$ADUser = Get-ADUser -Identity testuser1
$GN = $ADUser.GivenName
$SN = $ADUser.Surname

$proxies = foreach ($domain in $domains) {"smtp:$GN.$SN@$domain"}

$ADUser | Set-ADuser -Add @{Proxyaddress = $Proxies}

Hi

Yes in my example has no set command, it was example how to create that mail address. You had those set commands in your previous scripts. :slight_smile:

Jake

Oh ok, I see. :slight_smile:

I was actually able to get this working last night. I needed to lose the parenthesis around the givenname and surname and pipe it to foreach, enclosing everything in squiggly brackets.

 Get-ADUser -filter {EmailAddress -like "*@testdomain2.com"} | foreach {Set-ADuser $_ -Add @{proxyAddresses = "smtp:" + $_.GivenName + '.' + $_.Surname + "@testdomain1.
com "}} 

Thanks again!

I managed an Office 365 migration and learned a lot about emailaddress and proxyAddress, this weekend.

This thread was very useful and helped me fight through problems from the default configuration, where the domain and forest functional levels are still 2003 (for now…)

Long story short - I am providing my code and saying thanks to you @skalizar and a post where Dave Wyatt had commented :smiley:

$list | foreach {$email = "@exampleDomain.com" 
$deal = "SMTP:$($_.samaccountname)$email"
start-sleep -Milliseconds 100
write-verbose "Check your work $deal" -Verbose
set-ADUser -Identity $_ -Add @{ProxyAddresses= $deal} -WhatIf
$deal = $nulll}