Thursday, May 3, 2012

Windows User Account Management using the Command Line

Some friends of mine asked me to provide them one of my scripts that does the following things:
  1. Creates a user account on the local computer.
  2. Creates the user account’s password.
  3. Adds the user to the local administrator group.
First, I will show you the command (or line of code from the script), then break down what each piece performs. Here is the line:


net user USERNAME PASSWORD /add && net localgroup administrators USERNAME /add


This line of code is actually two different commands in one. The double ‘and’ (&&) joins both commands as if it is one. The Windows command line processes each command in order. As you can see, these commands are very straightforward. It uses the built-in NET USER and NET LOCALGROUP functions from the Windows command line. You can use this in your batch file scripts to perform some administrative tasks regarding local user accounts.

-Joe

2012-05-21 UPDATE: If planning on using these commands in an MDT Task Sequence, you must separate each command into their own task. For example, task #1 would be the command to create the user account and task #2 is the command to add that user account to the local security group. Failure to separate the CMD commands will cause the task to fail when the sequence is reached. [Shoutout to Brian for this tip!]