E Singh
E Singh

Reputation: 17

CMD Command for checking if Local User account exists and create if doesn't

I am looking a single line CMD command to check if local user exists & create if it doesn't exist.

I am aware about command to create a user:

"net user UserName Password /ADD"

As well as for checking if the user exists or not:

"net user | find /i UserName"

Though trying to see if there is a way to join these 2 commands to yield the desired result.

Any guidance would be greatly appreciated.

Upvotes: 0

Views: 1835

Answers (1)

lit
lit

Reputation: 16256

How about using a condition based on whether the user exists?

SET "THEUSER=username"
NET USER "%THEUSER%" 1>NUL 2>&1 || NET USER "%THEUSER%" Password /ADD

Upvotes: 2

Related Questions