View Categories

How to change a user UPN in Office 365 with PowerShell

4 min read

UPN Change via Microsoft Graph #

1. Make sure you are running the latest version of PowerShell.

2. Ensure you allow the running of scripts in PowerShell. The following command will allow scripts to run in your current user context

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

3. Install the Microsoft Online module

Install-Module -Name MSOnline

Connect to Microsoft Online

Run the following command to connect to Microsoft Online. You will be prompted to sign-in on a new pop-up window. Ensure you log in with an account with the necessary permissions to make this change, the required permissions are User Admin or Global Admin.

Connect-MSOLservice

Changing the UPN for a single user

Firstly identify the UPN you wish to change. In this scenario, we will change the UPN of user1 from [email protected] to [email protected].

Set-MsolUserPrincipalName -UserPrincipalName "[email protected]" -NewUserPrincipalName "[email protected]"

Now check your change has worked. For this will use the get-msoluser command with the -SearchString parameter.

get-msoluser -SearchString "User-1"

How to change a users UPN in Office 365 with Microsoft Graph PowerShell

1. Start by installing the Microsoft Graph PowerShell module.

2. Use the following command to change the UPN of a user.

Update-MgUser -UserId #OLDUPN '
-UserPrincipalName "#NEWUPN"

You can use the above command to either change the prefix, domain or both

Microsoft Graph Install #

To check which version of PowerShell you have installed, open PowerShell on your workstation and type the following command: $PSVersionTable.PSVersion

You can see from the screenshot, I am running version 5.1. As PowerShell 7 or later is recommended, I am going to update to the latest version. You will need to be a local administrator on your workstation to complete this step.

Run the following command to download and install the latest version of PowerShell 7 from the official GitHub repository.

iex “& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI”

When the installation wizard launches, click Next.

Choose the installation folder (I recommend leaving the default) and click Next.

Leave the Optional Actions page as default and click Next.

I recommend you leave the update options as default and then click Next.

Lastly, click Install.

Once the installation completes, close your existing PowerShell window and search for PowerShell 7 in your start menu.

Open PowerShell 7 to confirm the installation was successful.

Install Microsoft Graph PowerShell

The Install-Module cmdlet should be used to install the Microsoft Graph PowerShell module for either all users on your system or only the currently logged-in user.

Use the below examples to install the latest generally available (stable) version of the Microsoft Graph PowerShell module from the PowerShell Gallery.

To install the module for the current user scope:

Install-Module Microsoft.Graph -Scope CurrentUser

Or to install for all users on your system: (you will need local admin rights on your system):

Install-Module Microsoft.Graph -Scope AllUsers

The above commands can both be run in PowerShell 5.1 or PowerShell 7; however, installing one will not install it for the other.

You can also use the -allowprelease parameter to install preview versions of the module. This is useful if the preview version contains functions and commands that are not in the latest stable version.

The this following command to install the latest pre-release version of the module:

Install-Module Microsoft.Graph -Scope AllUsers -allowprerelease

You can confirm which version has been installed with the following command:

Get-InstalledModule | Where-Object {$_.Name -match "microsoft.graph"}

Your results should look like the following:

Connect to Microsoft Graph with PowerShell

Now you have installed the required module you can use the Connect-MgGraph cmdlet to connect to your environment from within PowerShell.

However, when connecting to Microsoft Graph with just Connect-MgGraph and no additional parameters, we will only have access to our own user account in the PowerShell session. As such, you must supply the needed permission scopes when connecting to your environment. For Example:

If you want ‘read’ and ‘write’ access to all user accounts in your tenant:

Connect-MgGraph -Scopes "User.Read.All","User.ReadWrite.All"

Or if you want ‘read’ and ‘write’ access to groups:

Connect-MgGraph -Scopes "Group.ReadWrite.All"

Vorgehensweise nach Installation von MS Graph: #

Connect-MgGraph -Scopes „Directory.AccessAsUser.All“ -Hiermit verbindet man sich mit den nötigen Zugriffsrechten

Update-MgUser -UserId ’[email protected]‘ -UserPrincipalName ’[email protected]‘ – Hier ändert man entsprechend den UPN