[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Create users from CSV file

Posted on 2009-02-17
Medium Priority
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 User.sn <> "" And User.givenName <> "" Then
            RptFile.WriteLine User.sAMAccountName & ", " & User.givenName & ", " & User.sn
        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
  • 2

Author Comment

ID: 23706653
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

RobSampson earned 1000 total points
ID: 23708325
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

ID: 31548064
Thank you Rob. Sorry for the delay!

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 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