Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

How to change the local user account password using PowerShell?



To change the local user account password using PowerShell, we can use the Set-LocalUser command with the Password parameter. This password parameter should be in the secure string. So we need to ask the user to input the password as a secure string or need to explicitly convert the plain text password to the secure string. For example,

$localuser = Read-Host "Enter Local UserName"
$password = Read-Host "Enter local user account password "
-AsSecureString
Set-LocalUser -Name $localuser -Password $password -Verbose

If you need to set the password without asking the user prompt then you need to convert the plain text password to the secure string forcefully as shown below.

$password = "Admin123" | ConvertTo-SecureString
-AsPlainText -Force
Set-LocalUser -Name TestUser -Password $password -Verbose

To set the local user password on the remote computer, use Invoke-Command.

Invoke-Command -ComputerName Computer1, Computer2 -ScriptBlock{
   $password = "Admin123" | ConvertTo-SecureString -AsPlainText -Force
   Set-LocalUser -Name 'TestUser' -Password $password -Verbose
}

The above command will set the local user account password on the remote servers computer1 and Computer2.

Updated on: 2021-05-17T13:39:09+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements