Copy 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.

' This code copies the attributes in the Attrs array from an 
' existing object to a new one.
' ------ SCRIPT CONFIGURATION ------
arrAttrs        = Array("department","co","title","l", "c", "st")
strParentDN     = "<ParentContainer>"   ' e.g. cn=Users,dc=rallencorp,dc=com
strTemplateUser = "<TemplateUserName>"  ' e.g. template-user-sales
strNewUser      = "<NewUserName>"       ' e.g. jdoe
strPassword     = "<Password>"
' ------ END CONFIGURATION ---------
     
Const ADS_UF_NORMAL_ACCOUNT = 512  ' from ADS_USER_FLAG_ENUM
     
Set objTemplate = GetObject("LDAP://cn=" & strTemplateUser & _
                            "," & strParentDN)
Set objParent   = GetObject("LDAP://" & strParentDN)
Set objUser     = objParent.Create("user", "cn=" & strNewUser)
     
objUser.Put "sAMAccountName", strNewUser
    
for each strAttr in arrAttrs
   objUser.Put strAttr, objTemplate.Get(strAttr)
next
     
objUser.SetInfo
objUser.SetPassword(strPassword)
objUser.SetInfo

objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT
objUser.AccountDisabled = FALSE
objUser.SetInfo

WScript.Echo "Successfully created user"

This code has been viewed 4802 times.

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