Adding to end of AD user Samaccountname

Brass Contributor

Hello I am trying to add a "1_" to the END of a list of Samaccountnames in AD. I found a script that will add it to the BEGINNING of the username but not the end. I have tried moving the 1_$ to then end of the last line but it does not work. Can anyone tell me how I can do this so the new Samaccountname is "aduser_1" instead of "1_aduser"? Below is the script I am using now. Thanks in advance!!


$storeusers = Get-Content -Path ".\users.txt"

ForEach ($storeuser in $storeusers) {

$ADuser = Get-ADUser $storeuser
Set-ADUser -Identity $aduser -SamAccountName "1_$($aduser.samaccountname)" -verbose



2 Replies
best response confirmed by charlie4872 (Brass Contributor)
You dont need to move the 1_$, just move the 1_ and make the code like this
Set-ADUser -Identity $aduser -SamAccountName "$($aduser.samaccountname)_1" -verbose
Thank you farismalaeb. That works!