When I went Googling for same sample code that would give me what I wanted, I found lots of examples showing how to create user accounts and a few showing how to create mailboxes. What I didn’t find was an example of how to create a large number of mailbox-enabled users. In the end I borrowed heavily from two examples: one from Robbie Allen’s Active Directory Cookbook and another from the Exchange Server Cookbook by Devin Ganger, Missy Koslosky and Paul Robichaux.
One thing to note is that the code must be run on an Exchange server or at least a machine that has the Exchange System Management Tools installed.
Also be aware that this is designed purely for testing. The password is the same for all accounts and the user attributes populated are the bare minimum.
Be aware that the Distinguished Name of the homeMDB is usually quite long and you will probably have to split it over several lines as I have done in the example above.
' This VBScript code creates a large number of users with incremented user names
' e.g. User1, User2, User3, ....
' The code also mailbox-enables the users
' ---------------------------------------------------------------
' The code includes extracts from the book "Active Directory Cookbook"
' by Robbie Allen
' and “Exchange Server Cookbook”
' by Devin Ganger, Missy Koslosky, and Paul Robichaux
' Publisher: O'Reilly and Associates
' ---------------------------------------------------------------
' ------ SCRIPT CONFIGURATION ------
intNumUsers = 50 ' Number of users to create
strParentDN = "OU=User Objects,DC=myco,DC=com" ' DN of where the users will be created
strHomeMDB = "CN=Mailbox Store (DCN1),CN=First Storage Group,CN=InformationStore,CN=DCN1," &_
"CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=myco," &_
"CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=myco,DC=com"
' ------ END CONFIGURATION ---------
' Taken from ADS_USER_FLAG_ENUM
Const ADS_UF_NORMAL_ACCOUNT = 512
set objParent = GetObject("LDAP://" & strParentDN)
for i = 1 to intNumUsers
strUser = "User" & i
strPwd = "Password1"
Set objUser = objParent.Create("user", "cn=" & strUser)
objUser.Put "sAMAccountName", strUser
objUser.SetInfo
objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT
objUser.SetPassword(strPwd)
objUser.AccountDisabled=FALSE
objUser.SetInfo
WScript.Echo "Created " & strUser
objUser.CreateMailBox strHomeMDB
objUser.SetInfo()
Wscript.Echo "Successfully mailbox-enabled user."
next
WScript.Echo ""
WScript.Echo "Created " & intNumUsers & " users"