We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Change user ID in Microsoft 365

May 18, 2021 Views: 12773

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, install Azure Active Directory PowerShell for Graph module on the 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 = "%firstname:lower%.%lastname:lower%@domain.com" # 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("https://graph.windows.net/")
$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
Leave a comment

Got questions?

Support Questions & Answers