Batch adding users to AD groups with a batch script

Posted by Dark Training on July 9, 2012 tags: | windows | batch

If you have ever had to add a bunch of AD users to groups in mass, doing it by hand would take hours. Below I show a script you can run to help automate the process.

FOR /F "tokens=1,2" %%A IN (C:\list.txt) DO ( 
dsquery user -samid %%B| dsmod group CN=%%A,OU=groups,DC=your,DC=Top,DC=domain -addmbr )

In this example I created text file in C:\ call "list.txt" with the names of the users that I wanted to insert into groups, then I simply create a batch (.bat) script with the code above and it will loop over each line, making the group in the default container.

The format of the list.txt in my example would be like this:

group1 bob
group2 bob
group2 sally

Hope this helps, if so leave a note below and let others know!