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

PowerShell: Practical Usage of Robocopy

Quick Commands: $source='C:\vssSnapshot_F'$destination='\\DESTINATIONSERVER\F$'robocopy $source $destination /E /R:0 /NP /XD '$RECYCLE.BIN' 'System Volume Information' /xf 'pagefile.sys'…

Query Print Spooler of SERVER1

function getStatus($server, $service){    Get-WmiObject Win32_Service -Filter "Name Like 'spooler'" -computer SERVER1 -credential $cred | select…

PowerShell: Benchmark Disk Speed

Contrary to previous iteration, this version returns an object with multiple properties describing statistical analysis…