Hello, Just looking for help please. I am new.

%3CLINGO-SUB%20id%3D%22lingo-sub-472248%22%20slang%3D%22en-US%22%3EHello%2C%20Just%20looking%20for%20help%20please.%20I%20am%20new.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-472248%22%20slang%3D%22en-US%22%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3EI%20am%20trying%20to%20get%20a%20script%20to%20update%20usernames%20in%20Azure%20AD.%20Below%20is%20the%20script%20I%20have%20created%2C%20I%20am%20new%20so%20I%20understand%20that%20I%20may%20be%20making%20it%20longer%20than%20needed.%20ANY%20suggestions%20on%20why%20its%20not%20working%3F%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3EThe%20results%20that%20I%20am%20receiving%20right%20now%3A%20when%20I%20run%20the%20%24upn%20variable%20I%20get%20the%20results%20I%20want%2C%20but%20when%20I%20run%20the%20%24newupn%2C%20the%20results%20I%20get%20are%20just%20the%20%40domain.com.%20It%20is%20not%20including%20the%20users%20first%20name%20like%20I%20want%20nor%20is%20it%20changing%20the%20users%20log%20on%20name.%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3EScript%3A%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3E%26lt%3B%23%20update%20the%20email%20addresses%20below%20to%20change%20from%20current%20username%20to%20new%20username%20%23%26gt%3B%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3E%24UserList%20%3D%20Get-AzureADGroupMember%20-ObjectId%20%22xxxxxxxxxxxxx%22%20%7C%20Get-AzureADUser%20%7C%20where%7B%24_.UserPrincipalName%20-like%20%22*.onmicrosoft.com%22%7D%20%7C%20Select-Object%20-ExpandProperty%20UserPrincipalName%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3E%24UserListFN%20%3D%20Get-AzureADGroupMember%20-ObjectId%20%22xxxxxxxxxxxxxx%22%20%7C%20Get-AzureADUser%20%7C%20Select-Object%20-ExpandProperty%20GivenName%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3E%26lt%3B%23Take%20%24UserList%20and%20update%20the%20Email%20address%20and%20login%20username%20for%20all%20objects%20in%20that%20list%23%26gt%3B%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3Eforeach(%24User%20in%20%24UsersList)%7B%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3E%24upn%20%3D%20%24UserList.UserPrincipalName%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3E%24newupn%20%3D%20%22%24(%24UserList.GivenName)%40domain.com%22%3C%2FP%3E%3CP%20class%3D%22s14dydj4-10%20kiAEUp%22%3ESet-AzureADUser%20-UserPrincipleName%20%24upn%20-NewUserPrincipleName%20%24newupn%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-472248%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Active%20Directory%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-472520%22%20slang%3D%22en-US%22%3ERe%3A%20Hello%2C%20Just%20looking%20for%20help%20please.%20I%20am%20new.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-472520%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20of%20all%2C%20the%20Set-AzureADUser%20cmdlet%20does%20not%20have%20a%20parameter%20called%26nbsp%3B%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20font-family%3A%20'SegoeUI'%2C'Lato'%2C'Helvetica%20Neue'%2CHelvetica%2CArial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3ENewUserPrincipleName%2C%20and%20you%20also%20have%20the%20wrong%20spelling%20for%20UserPrincipleName.%20Next%2C%20inside%20the%20loop%20you%20are%20referencing%20the%20full%20list%2C%20%24UserList%2C%20and%20not%20the%20current%20object%2C%20%24user.%20And%20you've%20complicated%20things%20a%20bit%20because%20of%20using%20multiple%20variables.%20Try%20something%20like%20this%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EGet-AzureADGroupMember%20-ObjectId%20XXX%20%7C%20%3F%20%7B%20%24_.ObjectType%20-eq%20%22User%22%20-and%20%24_.UserPrincipalName%20-like%20%22*.onmicrosoft.com%22%20%7D%20%7C%20%25%20%7B%20Set-AzureADUser%20-ObjectId%20%24_.ObjectId%20-UserPrincipalName%20%24_.UserPrincipalName.Replace(%22tenant.onmicrosoft.com%22%2C%22domain.com%22)%20%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-472838%22%20slang%3D%22en-US%22%3ERe%3A%20Hello%2C%20Just%20looking%20for%20help%20please.%20I%20am%20new.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-472838%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethank%20you%20for%20reaching%20out%20and%20helping%20me.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20that%20change%20just%20for%20grabbing%20the%20domain%20and%20then%20updating%20the%20domain%20or%20will%20it%20do%20the%20domain%20and%20the%20username%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-474296%22%20slang%3D%22en-US%22%3ERe%3A%20Hello%2C%20Just%20looking%20for%20help%20please.%20I%20am%20new.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-474296%22%20slang%3D%22en-US%22%3E%3CP%3EIt's%20pretty%20much%20doing%20the%20same%20thing%20as%20your%20original%20example.%20First%2C%20it%20gets%20all%20the%20members%20of%20a%20given%20group.%20Next%2C%20it%20filters%20out%20only%20the%20users%20(groups%20can%20have%20other%20object%20types%20as%20members%20too)%20and%20only%20the%20users%20with%20UPNs%20matching%20the%20tenant.onmicrosoft.com%20domain.%20Now%20that%20we%20have%20the%20list%20of%20all%20these%20user%20objects%2C%20we%20can%20proceed%20to%20changing%20their%20UPNs%20via%20the%20Set-AzureADUser%20cmdlet.%20And%20since%20we%20are%20using%20the%20full%20object%2C%20we%20can%20just%20reference%20any%20of%20its%20properties%2C%20without%20the%20need%20to%20use%20additional%20variables.%20Thus%20%24_.ObjectId%20will%20represent%20the%20ObjectID%20value%20of%20the%20currently%20processed%20user.%20Lastly%2C%20we're%20doing%20the%20replacement%20of%20the%20domain%20part%20of%20the%20UPN%20-%20using%20%24_.UserPrincipalName%20to%20get%20its%20current%20value%2C%20then%20using%20the%20Replace()%20method%20to%20swap%20the%20default%20domain%20name%20with%20the%20domain%20you%20want%20to%20use.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMakes%20sense%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-475042%22%20slang%3D%22en-US%22%3ERe%3A%20Hello%2C%20Just%20looking%20for%20help%20please.%20I%20am%20new.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-475042%22%20slang%3D%22en-US%22%3E%3CP%3EI%20realized%20I%20may%20have%20been%20too%20vague.%20right%20now%20the%20usernames%20are%20in%20the%20format%20firstname.lastname%40domain.onmicrosoft.com%20and%20what%20I%20want%20them%20to%20look%20like%20is%26nbsp%3B%3C%2FP%3E%3CP%3Efirstname%40domain.com%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESorry%20if%20I%20am%20being%20confusing%20I%20am%20pretty%20new%20to%20this...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-475747%22%20slang%3D%22en-US%22%3ERe%3A%20Hello%2C%20Just%20looking%20for%20help%20please.%20I%20am%20new.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-475747%22%20slang%3D%22en-US%22%3E%3CP%3EThen%20something%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EGet-AzureADGroupMember%20-ObjectId%20XXX%20%7C%20%3F%20%7B%20%24_.ObjectType%20-eq%20%22User%22%20-and%20%24_.UserPrincipalName%20-like%20%22*.onmicrosoft.com%22%20%7D%20%7C%20%25%20%7B%20Set-AzureADUser%20-ObjectId%20%24_.ObjectId%20-UserPrincipalName%20%24(%24_.FirstName%20%2B%20%22%40domain.com%22)%20%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
New Contributor

I am trying to get a script to update usernames in Azure AD. Below is the script I have created, I am new so I understand that I may be making it longer than needed. ANY suggestions on why its not working?

The results that I am receiving right now: when I run the $upn variable I get the results I want, but when I run the $newupn, the results I get are just the @domain.com. It is not including the users first name like I want nor is it changing the users log on name.

 

 

Script:

<# update the email addresses below to change from current username to new username #>

$UserList = Get-AzureADGroupMember -ObjectId "xxxxxxxxxxxxx" | Get-AzureADUser | where{$_.UserPrincipalName -like "*.onmicrosoft.com"} | Select-Object -ExpandProperty UserPrincipalName

$UserListFN = Get-AzureADGroupMember -ObjectId "xxxxxxxxxxxxxx" | Get-AzureADUser | Select-Object -ExpandProperty GivenName

<#Take $UserList and update the Email address and login username for all objects in that list#>

foreach($User in $UsersList){

$upn = $UserList.UserPrincipalName

$newupn = "$($UserList.GivenName)@domain.com"

Set-AzureADUser -UserPrincipleName $upn -NewUserPrincipleName $newupn

5 Replies

First of all, the Set-AzureADUser cmdlet does not have a parameter called NewUserPrincipleName, and you also have the wrong spelling for UserPrincipleName. Next, inside the loop you are referencing the full list, $UserList, and not the current object, $user. And you've complicated things a bit because of using multiple variables. Try something like this:

 

Get-AzureADGroupMember -ObjectId XXX | ? { $_.ObjectType -eq "User" -and $_.UserPrincipalName -like "*.onmicrosoft.com" } | % { Set-AzureADUser -ObjectId $_.ObjectId -UserPrincipalName $_.UserPrincipalName.Replace("tenant.onmicrosoft.com","domain.com") }

@Vasil Michev 

 

thank you for reaching out and helping me.

 

Is that change just for grabbing the domain and then updating the domain or will it do the domain and the username?

 

It's pretty much doing the same thing as your original example. First, it gets all the members of a given group. Next, it filters out only the users (groups can have other object types as members too) and only the users with UPNs matching the tenant.onmicrosoft.com domain. Now that we have the list of all these user objects, we can proceed to changing their UPNs via the Set-AzureADUser cmdlet. And since we are using the full object, we can just reference any of its properties, without the need to use additional variables. Thus $_.ObjectId will represent the ObjectID value of the currently processed user. Lastly, we're doing the replacement of the domain part of the UPN - using $_.UserPrincipalName to get its current value, then using the Replace() method to swap the default domain name with the domain you want to use.

 

Makes sense?

I realized I may have been too vague. right now the usernames are in the format firstname.lastname@domain.onmicrosoft.com and what I want them to look like is 

firstname@domain.com

 

Sorry if I am being confusing I am pretty new to this...

Then something like this:

 

Get-AzureADGroupMember -ObjectId XXX | ? { $_.ObjectType -eq "User" -and $_.UserPrincipalName -like "*.onmicrosoft.com" } | % { Set-AzureADUser -ObjectId $_.ObjectId -UserPrincipalName $($_.FirstName + "@domain.com") }