PowerShell: How to Change Active Directory Username

$oldUsername='testUser'
$newUsername='tUser'
$domainName='intranet.kimconnect.com'
$newUserPrincipleName="$newUsername@$domainName"

try{
    Set-ADUser $oldUsername -SamAccountName $newUsername -EA Stop
    write-host "$oldUsername has been changed to $newUsername"
    Set-ADUser $newUsername -UserPrincipleName $newUserPrincipleName -EA Stop
    write-host "$newUsername UserPrincipleName has been updated to $newUserPrincipleName"
}catch{
    write-warning $_
}

Leave a Reply

Your email address will not be published. Required fields are marked *