Create user (VBScript)

This code can be found in Chapter 6 of Active Directory Cookbook, 2nd edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' ---------------------------------------------------------------
' From the book "Active Directory Cookbook" by Robbie Allen
' ISBN: 0-596-00466-4
' ---------------------------------------------------------------

' Taken from ADS_USER_FLAG_ENUM
Const ADS_UF_NORMAL_ACCOUNT = 512 

set objParent = GetObject("LDAP://<ParentDN>") 
set objUser   = objParent.Create("user", "cn=<UserName>") ' e.g. joes
objUser.Put "sAMAccountName", "<UserName>"   ' e.g. joes
objUser.Put "userPrincipalName", "<UserUPN>" ' e.g. joes@rallencorp.com
objUser.Put "givenName", "<UserFirstName>"   ' e.g. Joe
objUser.Put "sn", "<UserLastName>"           ' e.g. Smith
objUser.Put "displayName", "<UserFirstName> <UserLastName>" ' e.g. Joe Smith

' CORRECTION: If you don't set userAccountControl, then by default
'             the value of 514 (normal account + disabled) will be set for it.
'             In this instance by setting it to 512, the account will not
'             be disabled, and if you have password complexity enabled in
'             your forest, the script will fail because a password was not
'             set prior to the account being enabled.  The solution is to 
'             not set userAccountControl here.
' objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT

objUser.SetInfo
objUser.SetPassword("<Password>")
objUser.AccountDisabled = FALSE
objUser.SetInfo

This code has been viewed 6335 times.

New from the creators of TechTasks.com: StatSheet.com