user8759102
user8759102

Reputation:

Powershell: New-LocalUser -A positional parameter cannot be found that accepts argument 'True'

I have been following this guide to try and write a powershell script to add a new local user to windows.

I need a username, fullname, description and password, and to check PasswordNeverExpires and UserMayNotChangePassword. I have written the following in an attempt to do so:

$Password = Read-Host -AsSecureString -Prompt 'Create password for new user account:'
New-LocalUser -Name "NewUser" -FullName "New User" -Description "New Execution Account" -Password $Password -PasswordNeverExpires $true -UserMayNotChangePassword $true

However, when running this I get the following error:

New-LocalUser : A positional parameter cannot be found that accepts argument 'True'. At line:2 char:1
+ New-LocalUser -Name "NewUser" -FullName "New User" -Description "New ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-LocalUser], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewLocalUserCommand

If anyone can see where I've gone wrong I would greatly appreciate any help or advice, thank you!

Upvotes: 4

Views: 4407

Answers (1)

Olaf
Olaf

Reputation: 5242

The last two parameters are switch parameters. You don't need to pass arguments to them. It's enough to simply provide them as they are. Like this:

$Password = Read-Host -AsSecureString -Prompt 'Create password for new user account:'
New-LocalUser -Name "NewUser" -FullName "New User" -Description "New Execution Account" -Password $Password -PasswordNeverExpires -UserMayNotChangePassword

Upvotes: 7

Related Questions