Not long ago I needed to change a company’s email address format from first initial last name@company.com (flast@company.com) to first name.last name@company.com (first.last@company.com). There were several hundred user accounts in the on-premises Active Directory. The local AD was being synchronized to Office 365 with Azure AD Connect.

Typically, an environment like this might have a stripped-down Exchange server configured for a hybrid federation with Office 365 to get access to the ECP (Exchange Control Panel). Having the ECP would have allowed me to use an address policy to solve this dilemma. However, this environment did not have such an option. Their available compute resources prevented the creation of a Exchange management server, so another solution was required.

As usual, I turned to PowerShell. I needed to use the name data in Active Directory to construct the email address. Then I had to insert the addresses into the ProxyAddress field so that they would be synchronized with the Office 365 mailboxes. Below is the code I came up with for accomplishing my goal.

Import-Module ActiveDirectory

$users = Get-ADUser -Filter * -SearchBase ("ou=users,dc=mydomain,dc=local")|select SammAccountName, Givenname, Surname

Foreach ($user in $users) 
{
     [string]$newproxyAddress = "SMTP:" + $user.Givenname + "." + 
     $user.Surname + "@" + "mydomain.com" 

     Set-ADUser -Identity $user.SamAccountName -Add 
     @{Proxyaddresses=$newproxyaddress}
}

This same code could easily be used to remove addresses by changing the -Add to -Remove. If you’re adding more than one address, the one labeled with a capital SMTP: will be the primary address. Set the others as a lowercase smtp:.

It worked like a charm. I ended up using this little snippet to build and insert three additional addresses for each of the users. In fact, it works so well, I will probably stop building Exchange servers in the local domain for managing email addresses and distribution lists.

Advertisements