Creating User Accounts in a subOU with VBScript

Posted on 2008-11-07
Last Modified: 2012-05-05
Okay, this is a stupid question. I have this VBScript to create user accounts and in it is a line that reads "strOU = "OU=Disabled User Accounts" ' Note the comma". I need to edit this line so that I can have the accounts created in a subOU. The way it's written now it will only find a top-level OU. For example, let's say I have an OU structure such that I have an OU named OU3 that's in OU2 which is in OU1 (OU1 is a top-level OU). I need for the accounts to be created in OU3. How do I edit that one line so that the accounts are in OU3? I thought I would just list the OUs in reverse order like this:

strOU = "OU=OU3,OU=OU2,OU=OU1"

I thought that's how you would edit that line but it's not working. Please help. Thanks.

Option Explicit

Dim objRootLDAP, objContainer, objUser, objShell

Dim objExcel, objSpread, intRow

Dim strUser, strOU, strSheet, strUPN, strDispName

Dim strCN, strSam, strFirst, strLast, strPWD, strDesc

strOU = "OU=UserAccounts" ' Note the comma

strSheet = "c:\useraccounts.xls"

' 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

' Trim function eliminates blank spaces

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

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

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

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

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

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

   strUPN = Trim(objExcel.Cells(intRow, 6).Value)

   strDispName = Trim(objExcel.Cells(intRow, 7).Value)

   strDesc = Trim(objExcel.Cells(intRow, 8).Value)


   ' Build the actual User from data in strSheet.

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

   objUser.sAMAccountName = strSam

   objUser.givenName = strFirst = strLast

   objUser.userPrincipalName = strUPN

   objUser.displayName = strDispName

   objUser.description = strDesc


   ' 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
    LVL 14

    Accepted Solution

    Try using this ...
    strOU = "OU=OU3,OU=OU2,OU=OU1,"
    Make sure there is a comma at the end of the string.

    I think that the current script is merging the strings so that it looks like this;
    As you can see, there whould be a comma between the OU1 and DC=

    Give that a try and let me know if that works for you.

    Author Closing Comment

    Thanks man, I know better than that. It's always something small that ends up missing.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    734 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

    24 Experts available now in Live!

    Get 1:1 Help Now