Create users from CSV file

Posted on 2009-02-17
Last Modified: 2012-06-21
I have a CSV file I created using the below code, from a production DC. I need to import that information into a DC in VMware.

How do I use VBScript to get the users and groups into AD?
Dim objFSO, strFileName, RptFile

Dim objRoot, objDomain

Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFSO = CreateObject("Scripting.FileSystemObject")

strDate = Left(TimeNow(Now), 4) & "-" & Mid(TimeNow(Now), 6, 2) & "-" & Mid(TimeNow(Now), 9, 2)

'Create folder on RMC R drive

If objFSO.FolderExists("C:\Temp2\Accounts\" & strDate) Then

	Set objFolder = objFSO.GetFolder("C:\Temp2\Accounts\" & strDate)


	Set objFolder = objFSO.CreateFolder("C:\Temp2\Accounts\" & strDate)

End If

strFileName="C:\Temp2\Accounts\" & strDate & "\All_AD_Output.csv"

Set RptFile = objFSO.OpenTextFile(strFileName, ForAppending, True)

Set objDomain = GetObject("LDAP://dc=MY,dc=Domain,dc=net") ' Used for a specific domain

' Set objRoot = GetObject("LDAP://RootDSE") 'Used with the line below

' Set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))



Set RptFile=Nothing

Set objUsers=Nothing

Set objOUs=Nothing

Set objDomain=Nothing

Set objRoot=Nothing

Set objFSO=Nothing

WScript.Echo "Done!"


Sub EnumOUs(adspath)

    Dim objOUs, OU

    Set objOUs = GetObject(adspath)

    objOUs.Filter = Array("OrganizationalUnit")

    Call EnumUsers(objOUs.ADsPath)

    For Each OU In objOUs

        'WScript.echo "Processing " & Replace(OU.Name, "OU=", "")

        ' RptFile.WriteLine Replace(UCase(OU.Name), "OU=", "")

        RptFile.WriteLine Replace(UCase(OU.distinguishedName), "OU=", "")

	Call EnumOUs(OU.ADsPath)


End Sub

Sub EnumUsers(adspath)

    Dim objUsers, User

    Set objUsers = GetObject(adspath)

    objUsers.Filter = Array("User")


    For Each User In objUsers

        If <> "" And User.givenName <> "" Then

            RptFile.WriteLine User.sAMAccountName & ", " & User.givenName & ", " &

        End If



End Sub

Function TimeNow(dDateTime)

      TimeNow =      Year(Now) & "-" &_

                        Right("00" & Month(Now), 2) & "-" &_

                        Right("00" & Day(Now), 2) & "-" &_

                        Right("00" & Hour(Now), 2) & "-" &_

                        Right("00" & Minute(Now), 2) & "-" &_

                        Right("00" & Second(Now), 2)

End Function

Open in new window

Question by:robby6161

    Author Comment

    I think I need to edit the question. We have a VM DC that we need for testing. I have manually created the VM DC and need to import the groups, usernames that I exported from the production DC with the attached code.

    Does anyone have any VB code that I can use to import that information into the VM?
    LVL 65

    Accepted Solution

    Hi there. There's not much point re-inventing the wheel by writing a script for this.  You can use Microsoft LDIFDE tool:

    Using LDIFDE to import and export directory objects to Active Directory

    LDIFDE - Export / Import data from Active Directory - LDIFDE commands



    Author Closing Comment

    Thank you Rob. Sorry for the delay!

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    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…
    This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
    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…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    755 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

    22 Experts available now in Live!

    Get 1:1 Help Now