TheGeekery

The Usual Tech Ramblings

Batch Adding AD Users

Part of the work I’m doing while I’m out had me add a collection of test users. This could have been quite a task, had it not been for the nice features of vbscript.

We’re performing some testing on one of our applications, and needed to batch add 100 users into Active Directory to perform the testing. This is the code I ended up using.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
dim strUser
Dim objRootLDAP, objContainer, objNewUser, objGroup


set objRootLDAP = GetObject("LDAP://rootDSE")
set objContainer = GetObject("LDAP://cn=Users," & _
   objRootLDAP.Get("defaultNamingContext"))

set objGroup = GetObject("LDAP://CN=App_Users,CN=Users,DC=testdmn,DC=com")


for i = 1 to 100

  strUser = "Test" & i
  set ObjNewUser = objContainer.Create("User", "cn=" & strUser)
  objNewUser.Put "sAMAccountName", strUser
  objNewUser.Put "cn", strUser
  objNewUser.Put "givenName", strUser
  objNewUser.Put "displayName", strUser
  objNewUser.Put "userPrincipalName", strUser & "@testdmn.com"
  objNewUser.SetInfo

  objNewUser.SetPassword("password")
  objNewUser.AccountDisabled = FALSE
  objNewUser.SetInfo

  objGroup.Add("LDAP://cn=" & strUser & ",CN=Users,DC=testdmn,DC=com")

  objNewUser = null

Next

It logins to the AD server, opens a connection, creates a user, adds them to the group, and moves onto the next user. When you open the AD Users & Computers now, you’ll see 100 Test# users.

Comments