Posted On June 24, 2021

PowerShell: How to Change Active Directory Username

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: How to Change Active Directory Username
$oldUsername='testUser'
$newUsername='tUser'

function changeUsername{
param(
$oldUsername,
$newUsername
)
$domainName=$env:USERDNSDOMAIN
$newUserPrincipleName="$newUsername@$domainName"
try{
    Set-ADUser $oldUsername -SamAccountName $newUsername -EA Stop
    write-host "$oldUsername has been changed to $newUsername"
    Set-ADUser $newUsername -UserPrincipalName $newUserPrincipleName -EA Stop
    write-host "$newUsername UserPrincipleName has been updated to $newUserPrincipleName"
}catch{
    write-warning $_
}
}

changeUsername $oldUserName $newUsername

Leave a Reply

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

Related Post

Basic JavaScript: Access Multi-Dimensional Arrays With Indexes

// Setupvar myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];// Using bracket notation select an…

PowerShell: Remove an A-Host Record within Active Directory Integrated DNS Domain

Warning: this code is NOT 'production ready'. Please review and test on DEV environments carefully…

Discover FSMO roles

Option ExplicitDim WSHNetwork, objArgs, ADOconnObj, bstrADOQueryString, RootDom, RSObjDim FSMOobj,CompNTDS, Computer, Path, HelpTextSet WSHNetwork = CreateObject("WScript.Network")Set…