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: Move Computer Objects in Active Directory

Version 2 # MoveComputers.ps1 # Version 0.0.2 # Obtain credentials being passed by Jenkins #…

Basic HTML and HTML5: Add a Submit Button to a Form

<h2>CatPhotoApp</h2><main><p>Click here to view more <a href="#">cat photos</a>.</p><a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying…

How To Create a Windows Scheduled Task to Call a Program or Script

Example on How To Call a Program: Set Action = Start a Program Set Program/Script…