Skip to content

Bulk edit Office 365 UPN

I have created a script to bulk edit the User Principal Name for users in Office 365.
The script compares a CSV filled with users that needs to edited and compares them to users in O365.

First connect to Microsoft Online environment.

# Import the MSOnline module.
Import-Module MSOnline

# Capture administrative credentials.
$Credential = Get-Credential

# Establishes Online Services connection to Azure Active Directory.
Connect-MsolService -Credential $Credential

Run the script to bulk edit the users in the CSV.

# Import users from CSV file.
$Users = Import-csv C:\temp\users.csv
#Get online users.
$MsolUsers = Get-MsolUser -All | Select-Object UserPrincipalName, ObjectId

ForEach ($MsolUser in $MsolUsers){

ForEach ($User in $Users){

#Edit "tenant.onmicrosoft.com" to desired tenant name or domain.

$newUPN = $User.UserPrincipalName.Split("@")[0] + "@tenant.onmicrosoft.com"

if ($MsolUser.UserPrincipalName -eq $User.UserPrincipalName){

Set-MsolUserPrincipalName -ObjectId $MsolUser.ObjectId -NewUserPrincipalName $newUPN -Verbose

}
}
}

The users in the CSV must be the UPN which the users have now. The $newUPN can be set the which ever UPN is wanted.

Published inOffice 365

Be First to Comment

Leave a Reply

Your email address will not be published.