Microsoft 365

Microsoft 365 | Azure AD : Changing User Logon Names (Bulk)

At some point your users have been uploaded via the Azure Active Directory Connect software toolkit. After some time you recognize that your users cannot use their common user names they are used to. They can only use the default onmicrosoft.com logon names. And finally you learn that you forgot to enter your custom domain (e.g. sid-500.com) during the configuration of AD connect. That’s annoying. In this blog post I will bail you out. With PowerShell, what else?

Ok, what I am talking about is this dilemma:

Anmerkung 2020-07-24 175634

Wait a minute you might now think you can change it directly in the users blade in your Microsoft 365 portal. Yes, that’s right. You can do it right here:

Anmerkung 2020-07-24 175823

But what if there are hundreds or thousands of users? Read on …

Connect to Azure  AD

Open PowerShell ISE or Visual Studio Code.

Enter the following lines to connect to your Microsoft 365 | Azure  AD tenant. Note that the first command is only necessary if no Azure AD module is installed.


Install-Module -Name AzureAD

Connect-AzureAD

Fine. Which brings me to the main point of this blog post.

Changing all Azure User Logon Names (Bulk)

The following lines change ALL user logon names to a domain name of your choice. Change line 5 to match your domain names.


$users = Get-AzureADUser | Select-Object -ExpandProperty UserPrincipalName

foreach ($u in $users) {

$new = $u -replace "sid500.onmicrosoft.com","sid-500.com"

Set-AzureADUser -ObjectId $u -UserPrincipalName $new

}

That looks good.

Changing Logon Names only for Users of a specific Group (Bulk)

For changing the user logon names for users in a specific group, retrieve all group members and then run the code.

First, get the ObjectId of the group.


Get-AzureADGroup | Sort-Object DisplayName

Anmerkung 2020-07-24 173831

Write down the groups ObjectId.

Then run the following code to change the logon names of all users in that group. Customzie line 1 and enter your groups ObjectId. Keep also an eye on line 6 and replace the values with your domain names.


$group = Get-AzureADGroupMember -ObjectId 4d530d96-c1b6-4e64-9386-93b050c40e7e |
Select-Object -ExpandProperty UserPrincipalName

foreach ($u in $group) {

$new = $u -replace "sid500.onmicrosoft.com","sid-500.com"

Set-AzureADUser -ObjectId $u -UserPrincipalName $new

}

Mission accomplished.

Hope this was helpful.

3 replies »

  1. Hi!

    I tried your smal script on my porject. The result was, that only the first 100 USers was set correctly to the new UPN.
    After rounabout one hour I found a solution.

    Just use the Following Command in the first row if the script

    $users = Get-AzureADUser -All $true | Select-Object -ExpandProperty UserPrincipalName

    That worked 🙂

    Like

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.