How to create a new local user in windows using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To create a new local user in the Windows operating system using PowerShell, we can use the New-LocalUser cmdlet. The below command will create the TestUser with no password.

New-LocalUser -Name TestUser -NoPassword

Output

Name Enabled Description
---- ------- -----------
TestUser True

TestUser account has been enabled here. To provide the password for the user, the password should be in the secure string format. We can pass the password as shown below.

$pass = "Admin@123" | ConvertTo-SecureString -AsPlainText
-Force
New-LocalUser -Name TestUser -Password $pass

The above commands will create the TestUser with the password. To add the password and account-related settings we can directly provide parameters but for ease, we will use the splatting method as shown below.

$Localuseraccount = @{
   Name = 'TestUser'
   Password = ("Admin#123" | ConvertTo-SecureString -AsPlainText -Force)
   AccountNeverExpires = $true
   PasswordNeverExpires = $true
   Verbose = $true
}

New-LocalUser @Localuseraccount

The above command will create testuser with a password and set its property to Account Never Expires and Password Never Expires.

raja
Published on 17-May-2021 12:12:31
Advertisements