Adding to all user accounts with PowerShell

When migrating Exchange on-premises to Exchange Online (Office 365) it is common to run into the follwing error message when starting a migration batch.

The target mailbox doesn’t have an smtp proxy matching ‘<domain>’

The <domain> proxy address in critical for mail routing to work in the hybrid scenario. Without the alias present on the user object the users that remain on-premises would not be able to send a message to any of the users located in the cloud.

While the issue can be fixed by modifying each users in “Active Directory Users and Computers” for large workloads this is definitly something you’ll want to accomplish using PowerShell.

The script below will go through all users in Active Directory and add the required alias. The added alias will have the format <SamAccountName>@<domain>

$Domain = "<domain>"

#Get all users in ActiveDirectory
$Users = Get-ADUser -Filter * -Properties ProxyAddresses

#Some output is always nice
Write-Host "Processing $Users.Count users..." -ForegroundColor Green

#Go through all users
foreach ($User in $Users) {

#Check if <domain> alias is present, if not add it as an alias
if ($User.Proxyaddresses -like "*$Domain*") {
Write-Host "$User.SamAccountName has an alias matching $Domain..." -ForegroundColor Yellow 
else {
$Alias = "smtp:" + $User.SamAccountName + "@" + $Domain
Set-ADUser $User -Add @{Proxyaddresses="$Alias"}
Write-Host "Alias addded to $User.SamAccountName..." -ForegroundColor Green
Write-Host "Done" -ForegroundColor Green

After running the script ensure that the changes are replicated to the domain controller used by Azure AD Connect. Wait for Azure AD sync to occur or start it manually.

After the sync the errors should be gone. If you still receive the same error, give Exchange Online 5-15 minutes to ensure the changes are properly propagated before going into a troubleshooting frenzy.

5 thoughts on “Adding to all user accounts with PowerShell”

  1. Had to tweak it slightly to add the smtp: before the proxy address. AAD sync was ignoring the new proxy addresses without it.

    Set-ADUser $User -Add @{Proxyaddresses=”smtp:$Alias”}

    1. You’re absolutely right, David. Thanks for pointing that out! I’ve updated the script accordingly.

    2. $Alias = “smtp:” + $User.SamAccountName + “@” + $Domain
      Set-ADUser $User -Add @{Proxyaddresses=”smtp:$Alias”}

      I had to remove $Alias = “smtp:” from the first line as I was getting a double up with the proxy address=”smtp:$alias addition in the next row.

      Cheers for the script though 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *