Changing Default Usernames and E-Mail in M365

**DISCLOSURE: While I am contracted to Microsoft Corporation, I am not an employee. The articles that I write are not meant to represent the company, nor are they meant to represent me as an employee or spokesman for the company. As has always been the case, all articles on this website represent me and nobody else.

**NOTE: All of the command line entries in this article are performed in PowerShell. To differentiate between the PowerShell cmdlets and Command Line Interpreter commands, the PowerShell cmdlets are in blue, and the Commands are in black.

Congratulations! You have taken the plunge, your company is enrolled in Microsoft 365. The cloud is the place to be! You start off by creating a bunch of user accounts for everyone. You then realize that you did not change the default domain, and everyone’s account is listed with an @<tenant>.onmicrosoft.com username and e-mail address. You want your people representing your brand, so that will not do.

image

PSOf course, you can go through the GUI, fixing each one individually… but at a certain point all of that mouse clicking will lead to carpal tunnel syndrome. I would much rather get it done in PowerShell.

The first thing we are going to have to do is to install the necessary PowerShell modules. If you have not done this yet, you need to do it from an Administrator Console:

Install-Module -Name MSOnline
Install-Module -Name AzureAD
Install-Module -Name ExchangeOnlineManagement

Now that you’re done with that, you likely won’t need to be in an Admin console, so you can close that and open a regular context PowerShell window.

We have to connect PowerShell to the Microsoft 365 tenant we are working on:

connect-msolservice

image

Enter your administrator username, and when prompted your password.

As strange as it may seem, in order to change e-mail addresses we also have to connect to Microsoft Exchange Online. Use the following script to do that:

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session –DisableNameChecking

Again, you will be prompted for your credentials.

Okay, you are connected to both Microsoft 365 and Microsoft Exchange Online. Let’s change those accounts:

Set-Mailbox -identity mgarvis@Behike.OnMicrosoft.com -WindowsEmailAddress mgarvis@behike.ca
Set-MsolUserPrincipalName -UserPrincipalName mgarvis@behike.onmicrosoft.com -NewUserPrincipalName mgarvis@behike.ca

image

If you don’t trust me, you can go into the GUI to check:

image

Great, it worked for a single user… but I want to do this for several users. No problem!

Step 1: Create a CSV file with all of the email addresses. It can be very simple, including just the User PrincipalName and the NewEmailAddress… like the following:

UserPrincipalName,NewEmailaddress
alexw@behike.onmicrosoft.com,AlexW@behike.ca
alland@behike.onmicrosoft.com,alland@behike.ca
diegos@behike.onmicrosoft.com,diegos@behike.ca
isaiahl@behike.onmicrosoft.com,isaiahl@behike.ca
jonis@behike.onmicrosoft.com,jonis@behike.ca
lynner@behike.onmicrosoft.com,lynner@behike.ca
meganb@behike.onmicrosoft.com,meganb@behike.ca
nestorw@behike.onmicrosoft.com,nestorw@behike.ca
pattif@behike.onmicrosoft.com,pattif@behike.ca

Now, run the following script (replacing the file location and name with your own)

$csv = Import-Csv C:\Users\v-mgarvis\Documents\Users.csv | ForEach-Object {
Set-Mailbox $_.”UserPrincipalName” -WindowsEmailAddress $_.”NewEmailAddress” -MicrosoftOnlineServicesID $_.”NewEmailAddress”
}

You should receive an output like the following:

image

**NOTE: Prior to running this script, I had reset all of the @behike.ca addresses back to their original @behike.onmicrosoft.com addresses. Otherwise there would be more errors here.

Yes, I removed Nestor’s license before running the script. Because there is no e-mail box, there is no e-mail address, so his entry failed… but as for the rest:

image

I hope this helps. While researching this article, I found a lot of them that assumed you knew this or that, such as what the format of the CSV file should be. I assume you are a newbie, and are looking for a beginner’s guide. If I missed anything, please drop a comment and I will try to help!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s