Recently, I assisted a client in securing their Windows 11 workstations by implementing effective password management for local user accounts. While many administrators rely on the graphical user interface (GUI) for this task, I find that PowerShell offers greater flexibility and efficiency.
In this article, I will share multiple methods to set or change passwords for local user accounts in Windows 11 using PowerShell. Whether you’re managing a single computer or hundreds across your organization, these techniques will be helpful.
Method 1: Using the Set-LocalUser Cmdlet
The best way to set a password for a local user in Windows 11 is to use the Set-LocalUser cmdlet. This cmdlet is part of the Microsoft.PowerShell.LocalAccounts module, which comes pre-installed in Windows 11.
Here are the steps:
- Open PowerShell as Administrator (right-click on the PowerShell icon and select “Run as administrator”)
- Use the following command syntax:
$Password = Read-Host -AsSecureString "Enter the new password"
Set-LocalUser -Name "UserName" -Password $PasswordLet’s say you want to set a password for a user named “Bijay”:
$Password = Read-Host -AsSecureString "Enter the new password"
Set-LocalUser -Name "Bijay" -Password $PasswordWhen you run this command, PowerShell will prompt you to enter the password. The characters won’t be visible as you type for security reasons. Once entered, press Enter, and the password will be set.
Check out Set the Time Zone Using PowerShell in Windows
Method 2: Using Net User Command
If you prefer a more traditional approach, you can use the net user command within PowerShell. While this isn’t a PowerShell cmdlet itself, it works perfectly well in the PowerShell console.
Here’s how to do it:
net user UserName NewPasswordFor example, to set the password for user “SarahJones” to “P@ssw0rd123”:
net user SarahJones P@ssw0rd123Note: This method isn’t as secure as Method 1 because the password is visible in plaintext. If someone is looking over your shoulder or if you’re recording your session, the password will be exposed.
Method 3: Using New-LocalUser to Create User with Password
If you’re creating a new user and want to set their password right away, you can use the New-LocalUser cmdlet:
$Password = Read-Host -AsSecureString "Enter the password"
New-LocalUser -Name "UserName" -Password $Password -FullName "Full Name" -Description "Description"For example, to create a new user named “MikeSmith” with a password:
$Password = Read-Host -AsSecureString "Enter the password"
New-LocalUser -Name "MikeSmith" -Password $Password -FullName "Mike Smith" -Description "IT Support Specialist"Check out Update PowerShell on Windows 11
Method 4: Batch Password Setting for Multiple Users
If you need to set passwords for multiple users at once (perhaps in a corporate environment), you can use PowerShell to automate this process:
$UserList = @("User1", "User2", "User3")
$Password = Read-Host -AsSecureString "Enter the password for all users"
foreach ($User in $UserList) {
Set-LocalUser -Name $User -Password $Password
Write-Host "Password set for $User" -ForegroundColor Green
}This script will set the same password for all users in the list. If you need different passwords for each user, you’ll need to modify the script accordingly.
Method 5: Setting Password with Additional Account Properties
Sometimes, you might want to set a password and modify other account properties simultaneously. Here’s how to do that:
$Password = Read-Host -AsSecureString "Enter the new password"
Set-LocalUser -Name "UserName" -Password $Password -AccountNeverExpires -PasswordNeverExpires $trueThis command sets the password and also configures the account so that the password never expires.
Check out Windows Terminal vs PowerShell
Method 6: Using a Script for Enterprise Deployment
For enterprise environments, you might want to create a reusable script to set passwords:
function Set-UserPassword {
param (
[Parameter(Mandatory=$true)]
[string]$Username,
[Parameter(Mandatory=$true)]
[securestring]$Password
)
try {
Set-LocalUser -Name $Username -Password $Password
Write-Host "Password for user $Username has been set successfully." -ForegroundColor Green
}
catch {
Write-Host "Error setting password for $Username: $_" -ForegroundColor Red
}
}
# Example usage:
$NewPassword = Read-Host -AsSecureString "Enter the new password"
Set-UserPassword -Username "JaneDoe" -Password $NewPasswordThis function provides error handling and can be incorporated into larger automation scripts.
Read List Windows Features Using PowerShell
Best Practices for Password Management
When setting passwords for local users in Windows 11, keep these best practices in mind:
- Use strong passwords: Combine uppercase and lowercase letters, numbers, and special characters.
- Don’t store passwords in plain text: Always use the
-AsSecureStringparameter when possible. - Set appropriate password policies: Consider setting password expiration and complexity requirements.
- Document your password management procedures: Ensure your team follows consistent practices.
- Audit password changes: Keep track of when passwords are changed and by whom.
Check out Windows PowerShell vs CMD
Common Issues and Troubleshooting
Here are some issues you might encounter while working with these PowerShell cmdlets
Issue 1: “Access Denied” Error
If you receive an “Access Denied” error, ensure you’re running PowerShell as an administrator. Right-click on PowerShell and select “Run as administrator.”
Issue 2: User Account Not Found
If you get an error that the user account doesn’t exist, verify the username with:
Get-LocalUserThis will list all local user accounts on the computer.
Issue 3: Password Doesn’t Meet Requirements
If the password doesn’t meet the local security policy requirements, you’ll receive an error. Check your local password policy with:
net accountsThis command shows password requirements like minimum length and complexity.
I hope you found this article helpful for managing local user passwords in Windows 11 using PowerShell. Whether you’re an IT administrator managing multiple systems or just setting up your personal computer, these methods provide efficient ways to secure your user accounts.
I have explained here how to set a password for a local user in Windows 11 using PowerShell.
You may also like:
- Check for Windows Updates Using PowerShell
- Install Windows Updates Using PowerShell
- Install RSAT in Windows 11 Using PowerShell
- Set Password Never Expires for Local User Using PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.