I'm having SharePoint 2016 On-premise environment, now we have a requirement as follows.

we have nearly 100 SharePoint Groups, where we had already have many user's identity added as DOMAIN\username ,now we want to add their full Email ID [email protected] to connect the site with Azure AD. In other words, in each group we need to take a list of existing user IDs (DOMAIN\username)and add a new Identity( [email protected] )for each user in the same group.

Initially we started adding the IDs manually, but its time taking process.

can anyone please suggest a power shell script to complete this activity easily.

  • 1
    Is there a reason you're not using Move-SPUser?
    – user6024
    Jun 1, 2020 at 4:24
  • @TrevorSeward, thanks for responding, we want to keep the existing identities also for sometime, and I'm not sure, move-spuser can solve this, can you please explain with a sample code or with an example Jun 1, 2020 at 4:42

1 Answer 1


You might look into this script:

$newID = "%firstname:lower%.%lastname:lower%@example.com" # TODO: modify me

# Get the 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 Microsoft 365
$Credential = $Context.GetOffice365Credential()
Connect-MsolService -Credential $Credential

# Check whether the Microsoft 365 User ID differs from the new ID
$userM365 = Get-MsolUser -ObjectId $objectId
$m365Username = $userM365.UserPrincipalName

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

# Change the Microsoft 365 User ID
Set-MsolUserPrincipalName -ObjectId $objectId -NewUserPrincipalName $newID
$Context.LogMessage("Microsoft 365 User ID has been changed to: $newID", "Information")


Change user ID in Microsoft 365

  • thanks for replying, my site is an On-Premises environment what are changes need to be done in this script. Jun 1, 2020 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.