In this article:
– Change Local Administrator password using PowerShell on single computer.
– Change Local Administrator account password using PowerShell on multiple computer
There are multiple ways to change your local Administartor account password. In this article, I will show you how to change local Administrator Password using PowerShell command.
Before we continue. here are a nice little tips for your Windows System.
- You should always be careful regarding password change.
- To avoid being in trouble, you should change your password frequently. It reduces the chances of getting hacked.
- Use complex character combination for your password.
- Disable Administrator account and create another user with Administrator privileges. Having another user with Administrators privileges could help you in many case.
Change Local Administrator password using PowerShell on a single computer
Open PowerShell with elevated permission and run the command below
$password = Read-Host "Enter the new password:" -AsSecureString Get-LocalUser -Name "Administrator" | Set-LocalUser -Password $password
Change Local Administrator account password using PowerShell on multiple computer
Change Local Administrator password on multiple computer could take your time. Using powershell command can help you to finish this job easily. Below is the PowerShell command/script to do that based on the list of the computers.
Get Input Password
First step, we need to capture the password input and save it on the variable as secure string.
$Password = Read-Host "Enter the New Password" -AsSecureString
$ConfirmPassword = Read-Host "Confirm the New Password" -AsSecureString
$pwd1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([RunTime.InteropServices.Marshal]::SecureStringToBSTR($Password))
$pwd2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([RunTime.InteropServices.Marshal]::SecureStringToBSTR($ConfirmPassword))
if ($pwd1 -ne $pwd2){
write-host "The passwords are not Match. Exit the script"
exit
}
Get the list of the computers
Now, to get the list of the computers, we will use Get-Content cmdlet.
$Computers = $Get-Content -Path $FileLocation
For the $FileLocation variable, you can Open File Dialog Box or using static location.
Changing The Administartor Password on Multiple Computers
After we have a list of computers, we can use PowerShell looping to run password change command
foreach ($computer in $computers){ try{ $account = [ADSI]("WinNT://$computers/Administrator,user") $account.psbase.invoke("setpassword",$pwd1) Write-Host = "Password has been succesfully changed" }catch{ Write-Host "Oops, There is an error: $_" } }
If you like this article, please share, subscribe or you can follow our Facebook Page and Twitter.
Hello Ardian,
Thank you so much for the script, I found it really helpful.
If i may, I wanted to point out a small mistake on the script.
# $account = [ADSI](“WinNT://$computers/Administartor.user”) it should be a comma
# $account = [ADSI](“WinNT://$computers/Administartorm,user”)
Hello Ahmed,
Thank you. You were right. it should be a comma.
I’ve changed my post
Hi, I am new to PS, need some lowest level help… Re $Computers = $Get-Content -Path $FileLocation, accumulated questions as follows:
a/ if my file is at C:\PCList.txt, should I change to $Computers = $Get-Content -Path $C:\PCList.txt
b/ we have a local admin account called overseer, can we use this script to change the password for overseer – by replacing the Administrator with overseer – so the resulting script will be
# $account = [ADSI](“WinNT://$computers/overseer,user”)
Pls advise. Many thanks
Lenny
a:/ the command should be $computers = Get-Content -path “C:\PCList.txt”
b:/ you’re right.
Hello,
Thank you very much for the article.
A very newbie question:
When I input 2 or more hostnames on seperate lines, it does not work. Do I need it format the .txt file, problem of escape character?
I just wrote like:
MachineA
MachineB
Not working.
But with one hostname it works.
Thank you.
Bests.
Frank