Script Repository

Change user ID in Microsoft 365

February 12, 2021

You can use the script in business rules, custom commands and scheduled tasks to update Microsoft 365 (Office 365) user identifier.

For the script to work, you need to install Microsoft Azure Active Directory Module for Windows PowerShell on each computer where Adaxes service is running.


  • $newID - Specifies a template for the new Microsoft 365 (Office 365) identifier. In the template, you can use value references. Value references are replaced with values of the corresponding properties of the user on which the script is executed.
Edit Remove
$newID = "" # TODO: modify me

# Get Microsoft 365 Object ID
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")    
    $Context.LogMessage("The user doesn't have a Microsoft 365 account", "Warning")

# Connect to AzureAD
$token = $Context.CloudServices.GetAzureAuthAccessToken("")
$tenant = $Context.CloudServices.GetO365Tenant()
$credential = $tenant.GetCredential()
Connect-AzureAD -AccountId $credential.AppId -AadAccessToken $token -TenantId $tenant.TenantId

# Check whether new identifier differs from the current one
$userM365 = Get-AzureADUser -ObjectId $objectId
$m365Username = $userM365.UserPrincipalName

if ($m365Username -ieq $newID)
    return # No changes needed

# Change user identifier in Microsoft 365
Set-AzureADUser -ObjectId $objectId -UserPrincipalName $newID

Comments ( 0 )
No results found.
Leave a comment

Related Scripts