VBScript to create user accounts

Posted on 2008-11-07
Last Modified: 2012-05-05
I'm going to use the code below to create a bunch of user accounts. It obtains the data from an Excel spreadsheet I have. I'd like the script to set the option "User must change password at first logon" but don't know how. Can you tell me what I need to add to this script to configure that and where in the script it needs to go? Thank you.
' Script to create user accounts with. Obtains

' user information from an Excel spreadsheet

' ------------------------------------------------------' 

Option Explicit

Dim objRootLDAP, objContainer, objUser, objShell

Dim objExcel, objSpread, intRow

Dim strUser, strOU, strSheet, &_

  strCN, strSam, strFirst, strLast, strPWD

strOU = "OU=" & Inputbox("Enter the OU to add the users to:") & "," ' Note the comma

strSheet = Inputbox("Enter the path and filename of the Excel spreadsheet"

' Bind to Active Directory, Users container.

Set objRootLDAP = GetObject("LDAP://rootDSE")

Set objContainer = GetObject("LDAP://" & strOU & _


' Open the Excel spreadsheet

Set objExcel = CreateObject("Excel.Application")

Set objSpread = objExcel.Workbooks.Open(strSheet)

intRow = 3 'Row 1 often contains headings

' Here is the 'DO...Loop' that cycles through the cells

' Note intRow, x must correspond to the column in strSheet

Do Until objExcel.Cells(intRow,1).Value = ""

   strSam = Trim(objExcel.Cells(intRow, 1).Value)

   strCN = Trim(objExcel.Cells(intRow, 2).Value) 

   strFirst = Trim(objExcel.Cells(intRow, 3).Value)

   strLast = Trim(objExcel.Cells(intRow, 4).Value)

   strPWD = Trim(objExcel.Cells(intRow, 5).Value)

   ' Build the actual User from data in strSheet.

   Set objUser = objContainer.Create("User", "cn=" & strCN)

   objUser.sAMAccountName = strSam

   objUser.givenName = strFirst = strLast


   ' Separate section to enable account with its password

   objUser.userAccountControl = 512

   objUser.pwdLastSet = 0

   objUser.SetPassword strPWD


intRow = intRow + 1




Open in new window

Question by:mcpp661
    1 Comment
    LVL 13

    Accepted Solution


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
    Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now