How to change the local user account password using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

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.

raja
Published on 17-May-2021 13:35:25
Advertisements