rename local admin account on remote computers using powershell.

Hello,

I need a script to read a text file of computer names and rename the local administrator account to another name.  

I know group policy can do this but I'd like to use powershell.

May someone please advise?

thanks.
nav2567Asked:
Who is Participating?
 
nav2567Author Commented:
I am trying this code but it is not working yet.  Someone who knows please advise.  Thanks.

$computers = Get-Content -path C:\computers.txt
$localaccount = Read-Host -prompt "Enter new localaccount name for admin" -assecurestring
$decodedlocalaccount = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($localaccount))
Foreach($computer in $computers)
{
 $computer
 $user = [adsi]"WinNT://$computer/administrator,user"
 $user.SetInfo()
}
0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
Before any of that you first get the administrator account name (SID ending with -500) because it might be renamed already
0
 
nav2567Author Commented:
Not yet.  Here is the latest script I use but it is still not working

$computers = Get-Content -path C:\computers.txt
$accountname = Read-Host -prompt "Enter new accountname for administrator" -assecurestring
$decodedaccountname = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($accountname))
Foreach($computer in $computers)
{
 $computer
 $user = [adsi]"WinNT://$computer/administrator,user"
 $user.administrator.rename("newlocaladmin")
 $user.SetInfo()
 }
0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
Have you tested it locally? Did you all it through the firewall? You also need to add credentials to do it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.