Script to Add Users to Active Directory

Scripting is a system administrator’s best friend as it allows you to do very labor intensive and mundane tasks often without user intervention.  In working with an educational institution client recently, adding users to active directory using a script was something they wanted to be able to do.  They had access to student information via a SQL database due to their campus information system utilizing SQL as the backend of their CIS system.

By utilizing a SQL query, we were able to pull student information from the database and then feed that into a simple DOS “for” loop leveraging the dsadd command.

The code:

@echo on
SET INPUT=yourflatinputfile.txt

echo Creating Accounts

for /f “tokens=1-14” %%A in (%INPUT%) do (dsadd user >>log.txt “CN=%%A %%B,OU=Students,DC=test,DC=edu” -fn %%C -ln %%D -display “%%K %%L %%M” -mi %%L -samid %%F -upn %%G -mustchpwd yes -disabled no -desc %%H -pwd %%N -loscr %%J -profile %%I -memberof “CN=Students,DC=test,DC=edu”)

echo ————-



The input file needs to contain the following information in the above order.  Now, note, you can rearrange the above script to fit your needs, however, once you decide on how the script is going to read from your flatfile, you need to adjust it accordingly.  The input file is specified in the top of the script, so you will need to make sure the input file is in the same directory as the script, or specify explicitly where it resides.

The above script sets among other things, the display name, samid, password status, description, and group memberships.


Back to top button