Wednesday, May 31, 2017

The command line to add a local windows user with the Special Characters password

The command line to add a local windows user called “newuser” with the password “p&ssw^rd”
You try
1
net user newuser p&ssw^rd /ADD
Uh-oh – it fails!
1
2
3
4
5
6
7
C:\> net user newuser p&ssw^rd /ADD
The user name could not be found.
 
More help is available by typing NET HELPMSG 2221.
 
'sswrd' is not recognized as an internal or external command,
operable program or batch file.
If the password contains certain special characters – like an ampersand “&” or a caret “^”, the password will be garbled, broken, butchered.
One solution is to have it prompt for the password
1
net user newuser * /ADD
but if you are scripting, this isn’t really helpful.
No, you cannot use quotes.
The solution: All Ampersands must be escaped with a caret “^”, and all carets in the password must be similarly escaped.
UPDATE: turns out in more recent versions of windows, exclamation marks “!” must also be escape with two carets.
See here for a good list of how to escape things.
http://www.robvanderwoude.com/escapechars.php
So, to use the password p&ssw^rd in a command line, you would need to replace it with p^&ssw^^rd
1
net user newuser p^&ssw^^rd /ADD
This will do what you expect
Note that if you do not escape the carets, the command may succeed, but the password will be wrong.

No comments: