Forum Discussion
Hello, Just looking for help please. I am new.
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") }
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?
- VasilMichevApr 19, 2019MVP
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?
- Paul-ITApr 19, 2019Copper Contributor
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...
- VasilMichevApr 19, 2019MVP
Then something like this:
Get-AzureADGroupMember -ObjectId XXX | ? { $_.ObjectType -eq "User" -and $_.UserPrincipalName -like "*.onmicrosoft.com" } | % { Set-AzureADUser -ObjectId $_.ObjectId -UserPrincipalName $($_.FirstName + "@domain.com") }