Environment: Active Directory with Azure Active Directory Connect and Exchange Online
I had a job recently where I needed to change everyone's email address to a new domain but I couldn't accomplish this from Exchange Online as all the users are synchronized with Active Directory on-premises.
I wrote a quick script that addresses this:
<#
PowerShell
Objectives
– query AD with all users with a 'mail' attribute
– update the 'mail' attribute to the new domain
– set the proxyaddress SMTP matching the mail attribute to lowercase smtp
– set the proxyaddress SMTP to the new domainAuthor: Jason Zondag
Date: 2202.08.11
#>
# Set Variables
$newMailDomain = "newdomain.com"
$oldMailDomain = "olddomain.com"# Make sure we have AD access
Import-Module ActiveDirectory# Set the SearchBase
$SearchBase = "DC=domain,DC=local"# Get the required data and loop
$Users = Get-ADUser -Filter "mail -like '*'" -SearchBase "$SearchBase" -ResultSetSize $null -properties mail,proxyaddresses,samaccountname,givenname,sn
ForEach ($User in $Users)
Note – you must run this script with elevated privileges.
I would also recommend that you add the command to stop AAD Sync during the course of running the script.