Bulk update AAD/O365 UserPrincipalName Suffix with PowerShell

Tony S 96 Reputation points
2020-09-24T15:09:29.953+00:00

Long story short, we had some users that were created with our local UPN, dirsynced to O365/AAD, and then licensed with an E3 license. Their email address was incorrectly entered as what their login name is supposed to be, so when that was created, switching the UPN in AD doesn't update anything in O365/AAD because it says it's a duplicate.

I parsed down the data as much as possible to the 40 or so users that need their UPN suffix changed from the .onmicrosoft.com to our correct one. Problem is, I can't seem to figure out exactly how to pull the data out of the CSV.

When we do a single user, I use this exact line: Set-AzureADUser -ObjectId OldUsername@keyman .com -UserPrincipalName NewUsername@keyman .com

I have a CSV created with two columns. One is the current UPN, the other is the one I want going forward. I tried something like this:

Import-CSV "C:\UPNTest.csv" | Foreach { Set-AzureADUser -ObjectID $_UserPrincipalName -UserPrincipalName $_NewUPN }  

I tried it a few different ways and I get errors each way. I was under the impression that the "-Text" would pull from the corresponding CSV column and the "$_" would apply that to the field in Azure. I've tried each way and I get errors still.

Am I being too simplistic here because I have multiple variables and I need to define each one?

Thanks!

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,559 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tony S 96 Reputation points
    2020-09-24T17:05:28.94+00:00

    Oh my, I'm an idiot. I forgot "." between the "$_" and the "NewUPN"

    This worked:

    Import-CSV "C:\UPNTest.csv" | Foreach { Set-AzureADUser -ObjectID $_.UserPrincipalName -UserPrincipalName $_.NewUPN }
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Harvey Alexander 6 Reputation points
    2022-07-27T10:49:03.173+00:00

    For this bulk change is this using exchange online or MS online?

    Additionally is it just the @ssss .com that we include in the command as the names will be searched for in the CSV file?

    Thanks!

    1 person found this answer helpful.
    0 comments No comments

  2. Arun Siwakoti 0 Reputation points
    2023-12-17T12:18:23.9066667+00:00

    Hope it helped you :)