Creating multiple users using a batch file

This is probably an old topic but I'll blog it anyways in case someone might find it useful.

This article is about how to create a list of new users in a specific OU. So here are the requirements:

Users are required to change password at first log on
User accounts are enabled
Username would be the first initial + lastname
There will be a same default password assigned to all of the users

Steps:

Create a batch file with the following content

@echo off
cls
echo Creating Accounts
echo ------------------
for /f "tokens=1-3" %%A in (userlist.txt) do (dsadd user "CN=%%A,ou=Support,ou=IT,dc=thenguyen,dc=local" -fn %%B -ln %%C -display "%%B %%C" -upn %%A@thenguyen.local -pwd Passw0rd1 -mustchpwd yes -disabled no)
echo ------------------
pause

Create a text file call userlist.txt. Enter in the user's information as follow:
Username Firstname Lastname

Example:
pnguyen Peter Nguyen

Explanation - I will only explain what the for loop does since it's the main part of the batch file that loops through the userlist.txt file to create users

for /f "tokens=1-3" %%A in (userlist.txt) do:
 Will read through the userlist.txt file line by line using the "tokens" which are the Username Firstname Lastname elements in that text file. The %%A is a variable representing the first element of the line that was recently read which is token 1. For each of the %%A that was read, the For loop will perform the following:

dsadd user "CN=%%A,ou=Support,ou=IT,dc=thenguyen,dc=local" -fn %%B -ln %%C -display "%%B %%C" -upn %%A@thenguyen.local -pwd Passw0rd1 -mustchpwd yes -disabled no
dsadd user -  
tells the machine to prepare to add a user
CN=%%A,ou=support,ou=IT,dc=thenguyen,dc=local - In this example %%A would be pnguyen, and then ds add would put pnguyen in the "TheNguyen.Local\IT\Support" OU.
-fn %%B -ln %%C -display "%%B %%C" - FN specifies the first name of the user, in this case would be %%B (Peter) or the second token that was read in from userlist.txt. -LN %%C specifies the last name of the user, in this case would be %%C (Nguyen) or the third token that was read in from the file. -Display "%%B %%C" tells the command to use first name last name as the display name for the user
-upn %%A@thenguyen.local - Tells the command to use %%A (pnguyen) as the UPN or log on name of the user
-pwd Passw0rd1 -mustchpwd yes - Specifies the password is Passw0rd1 and that user must change password at next log on
-disabled no - ensures that user account is not disabled after its been created.

 del.icio.us  Stumbleupon  Technorati  Digg 

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this entry.
Leave a comment

Submitted comments will be subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.