Create many users (VBScript)

This code can be found in Chapter 23 of Active Directory, 3rd 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, Third Edition" 
' ISBN: 0-596-10173-2

Option Explicit

Const ForReading = 1

Dim objDomain, objUser, fso, tsInputFile, strLine, arrInput
Dim fldUserHomedir, wshShell

Set objDomain = GetObject("LDAP://cn=Users,dc=mycorp,dc=com")
Set fso = CreateObject("Scripting.FileSystemObject")

'**********************************************************************
'Open the text file as a text stream for reading.
'Don't create a file if users-to-create.txt doesn't exist
'**********************************************************************
Set tsInputFile = fso.OpenTextFile("c:\users-to-create.txt", ForReading, False)

'**********************************************************************
'Execute the lines inside the loop, as long as you're not at the end
'of the file
'**********************************************************************
While Not tsInputFile.AtEndOfStream

  '**********************************************************************
  'Read a line, and use the Split function to split the data set into
  'its separate parts
  '**********************************************************************
  strLine = tsInputFile.ReadLine
  arrInput = Split(strLine, ":")

  Set objUser = objDomain.Create("user","cn=" & arrInput(0))
  objUser.Put "sAMAccountName", arrInput(0)
  objUser.Put "userPrincipalName", arrInput(0) & "@mycorp.com"

  '**********************************************************************
  'Write the newly created object out from the property cache
  'Read all the properties for the object, including
  'the ones set by the system on creation
  '**********************************************************************
  objUser.SetInfo
  objUser.GetInfo

  '**********************************************************************
  'Set the password
  '**********************************************************************
  objUser.SetPassword arrInput(4)

  '**********************************************************************
  'Set the properties
  '**********************************************************************
  objUser.AccountDisabled = False
  objUser.AccountExpirationDate = arrInput(1)
  objUser.Description = arrInput(2)
  objUser.LoginScript = "\\MYDOMAIN\DFS\Loginscripts\" & arrInput(0) & ".vbs"
  objUser.Profile = "\\MYDOMAIN\DFS\Users\" & arrInput(0) & "\profile"
  objUser.PasswordRequired = True
  objUser.DisplayName = arrInput(3)

  '**********************************************************************
  'Set the drive that you'll map to
  '**********************************************************************
  objUser.HomeDirectory = "\\MYDOMAIN\DFS\Users\" & arrInput(0)
  objUser.Put "homeDrive", "Z:"
  objUser.SetInfo

  '**********************************************************************
  'Create the home directory
  '**********************************************************************
  If Not fso.FolderExists("\\MYDOMAIN\DFS\Users\" & arrInput(0)) Then
    Set fldUserHomedir = fso.CreateFolder("\\MYDOMAIN\DFS\Users\" & arrInput(0))
  End If

  '**********************************************************************
  'Set full rights for the user to the home directory
  '**********************************************************************
  Set wshShell = WScript.CreateObject("Wscript.Shell")
  wshShell.Run "cacls \\MYDOMAIN\DFS\Users\" & arrInput(0) _
    & " /e /g " & arrInput(0) & ":F", 1, True

  '**********************************************************************
  'Stop referencing this user
  '**********************************************************************
  Set objUser = Nothing
Wend

'Close the file
tsInputFile.Close

This code has been viewed 3525 times.

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