Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!


1 Answers

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
like image 123
Olaf Avatar answered Oct 18 '25 04:10

Olaf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!