Hybid Exchange – Bulk Email Domain Name Change

All Powershell Commands

RED text indicates the bits you change for your environment.


CSV File example

CSV has a header row as shown and each user on a new line. Save as, for example, C:\tmp\yymmdd.csv

John Smith,John.Smith,John.Smith@OLDdomain.com,John.Smith@NEWdomain.com


Import CSV
$users = Import-csv C:\tmp\yymmdd.csv


List AD user OU Location
$users | ForEach-object { Get-ADUser -filter "Name -like '*$($_. Displayname)'"  -Properties CanonicalName | select Name, SamAccountName, CanonicalName, DistinguishedName } | Export-Csv c:\tmp\usersOUs.csv


 Connect to MSOL


Get Tennant GUID
$tenid = (Get-MsolPartnerContract -domain bobbitco.onmicrosoft.com).tenantid.guid


Check accounts’ last dirsync time
$users | foreach-object {get-msoluser -TenantId $tenid -UserPrincipalName "$($_.primarysmtpaddress)" | select userprincipalname, lastdirsynctime } | out-file c:\tmp\lastsync.txt


Check Remote Mailboxes exist
$users | ForEach-object { $exist = [bool](Get-remotemailbox $_.primarysmtpaddress -erroraction SilentlyContinue); Write-host "$Exist, $($_. Displayname)" }


Add additional SMTP address
$users | ForEach-Object { Set-RemoteMailbox $_.SamAccountName -EmailAddresses @{Add=$_.NewEmail} }


Set Primary SMTP address and disable email address policy
$users | ForEach-Object { Set-RemoteMailbox $_.SamAccountName -PrimarySMTPAddress $_.NewEmail -EmailAddressPolicyEnabled $false }


$users | ForEach-Object { set-RemoteMailbox $_.SamAccountName -userprincipalname $_.NewEmail }


MSOL UPN update 1
$users | ForEach-Object { Set-MsolUserPrincipalName -TenantId $tenid -UserPrincipalName $_.PrimarySmtpAddress -NewUserPrincipalName ($_.SamAccountName+’@bobbitco.onmicrosoft.com’) }


MSOL UPN update 2
$users | ForEach-Object {  Set-MsolUserPrincipalName -TenantId $tenid –UserPrincipalName ($_.SamAccountName+’@bobbitco.onmicrosoft.com’) -NewUserPrincipalName $_.NewEmail }


Output MSOL UPNs
$users | ForEach-Object {get-msoluser -TenantId $tenid -UserPrincipalName $_.NewEmail | select userprincipalname} | out-file c:\tmp\OutputMSOLs.txt


Check Last Synctime
$users | ForEach-Object {Get-MSOLuser -TenantId $tenid -UserPrincipalName $_.newemail | select UserPrincipalName, LastDirsyncTime} | out-file c:\tmp\synctime.txt


2 thoughts on “Hybid Exchange – Bulk Email Domain Name Change

  1. Nice work Bobb. Two lines of your code seem to be identical though.
    “Add additional SMTP address” and “Set Primary SMTP address and disable email address policy”.

Leave a Reply