Skip to content

Instantly share code, notes, and snippets.

@iyre
Created February 20, 2020 15:20
Show Gist options
  • Save iyre/e6c1c24cf20d174186acea78cc3a9f0c to your computer and use it in GitHub Desktop.
Save iyre/e6c1c24cf20d174186acea78cc3a9f0c to your computer and use it in GitHub Desktop.
Batch replace domain name portion of UPN in active directory
# batch replace domain name portion of UPN in active directory
$SearchBase = "OU=Domain Users, DC=contoso, DC=com"
$OldDN = "contoso.com.local"
$NewDN = "contoso.com"
Import-Module ActiveDirectory
Get-ADUser -Filter { UserPrincipalName -like "*$OldDN" } -SearchBase $SearchBase |
ForEach-Object {
Write-Host $_.Name
$Upn = $_.UserPrincipalName.Replace( $OldDN, $NewDN )
Set-ADUser $_ -UserPrincipalName $Upn
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment