Hybid Exchange – Bulk Email Domain Name Change

Command Formatting

In the examples below I’ve highlighted in RED any part of a command that you may need to change to match your environment i.e. file locations, domain names and such.

Import CSV

Now open the Exchange Management Shell (EMS) and import the .CSV into memory using the following command:

$users = Import-csv C:\tmp\book1.csv


Verify CSV

You can check the file loaded okay by just typing:


And you should have something that looks like this:


Verify User List have O365 mailboxes

This command will check each name on the list and display a TRUE / FALSE on screen if there is a remote mailbox. If you get any FALSE’s you can investigate, via AD / EMC / O365.

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


Grab Existing User Email Addresses

Okay the next part is the clever bit, we’ll use a Powershell command via the EMS to list the users and pull the following details from the remote mailbox / AD; User display name, SAM account name, Primary SMTP address and any other SMTP addresses and place them into a CSV file called rollback.csv. This file can be used as a data source should you ever want to return a user to their original state.

We’ll also use this CSV to construct another CSV that we’ll use to make the final changes.

$users | ForEach-Object {Get-remoteMailbox $_.User -ResultSize Unlimited |Select-Object DisplayName,SamAccountName,PrimarySmtpAddress, @{Name="EmailAddresses";Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq "smtp"} | ForEach-Object {$_.SmtpAddress}}} } | export-csv -NoTypeInformation c:\tmp\rollback.csv


Save rollback.csv Just in Case

Save the rollback.csv somewhere safe and then copy the file to your workstation and open it in excel and it should look like this. Notice the EmailAddresses column features your <domain>.mail.onmicrosoft.com – remember this you’ll need it later!

Note: the rest of this article assumes the email addresses (old and new) are of the format samaccountname@domain.com (i.e. first.last@domain.com).


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