Forum Discussion
TomWechsler
Apr 05, 2021MVP
Using PowerShell to change the User Principal Name (UPN) for a user in Active Directory!
Dear Windows Active Directory friends, I am absolutely aware that there are probably already a lot of articles on this topic. Nevertheless, I would like to show you my steps how I did this in ...
jkariuki
Jul 14, 2023Copper Contributor
how can you change a username without affecting the domain name using a script?TomWechsler
- LainRobertsonJul 15, 2023Silver Contributor
There's many ways to do this and your question does not provide enough information for us to provide a concise answer.
If you can provide more information on what you're trying to achieve, such as examples values for:
- A current userPrincipalName;
- The new format of userPrincipalName;
- A description on how you anticipate getting from the current to new formats.
Then we can provide more concise advice.
Here's a generic example on how you can change the prefix (username) component of the userPrincipalName without changing - or even needing to know anything about - the suffix (domain) component.
# Get the Active Directory object to be changed. $UserPrincipalName = ($User = Get-ADObject -Filter { userPrincipalName -eq "email address removed for privacy reasons" } -Properties userPrincipalName).userPrincipalName; # Split it into it's separate components (i.e. username and FQDN.) $Parts = $UserPrincipalName.Split("@"); $Username = $Parts[0].ToLowerInvariant(); $Domain = $Parts[1]; # Make some kind of change to the username component. In this case we're going to switch from the current [givenName].[sn] format to [givenName initial][surname]. $UsernameParts = $Username.Split("."); $Username = $UsernameParts[0][0] + $UsernameParts[1]; # Set the new value of userPrincipalName. Remove the "-WhatIf" parameter to actually make the change (I'm clearly not interested in changing my userPrincipalName.) $NewUserPrincipalName = "$Username@$Domain"; Write-Warning -Message "Changing userPrincipalName from $UserPrincipalName to $NewUserPrincipalName..."; Set-ADObject -Identity ($User.ObjectGUID) -WhatIf -Replace @{ userPrincipalName = $NewUserPrincipalName; }
Here's the output from that example script (noting I'm using -WhatIf to avoid effecting the change.)
Cheers,
Lain