Search code examples
windowspowershellpowershell-2.0powershell-3.0powershell-4.0

How to amend this powershell script to include the password?


The below code will prompt the user for the password but I want to insert a password in the script (hidden would be preferable). Therefore, the script will run with user inervention and will create and add the user to the admin group.

$Password = Read-Host -asSecureString

New-LocalUser -Name User -password $Password  

Add-LocalGroupMember -Group "Administrators" -Member User

I modified the script to the below, however I received a secure script error:

New-LocalUser -Name User -password Password  

Add-LocalGroupMember -Group "Administrators" -Member User

Solution

  • You can use ConvertTo-SecureString -AsPlainText -Force to convert a string value to a [SecureString] instance:

    $Password = "th3d3f4ultP4$$W0rd!!!" |ConvertTo-SecureString -AsPlainText -Force