0
CSV file with this VBScript January 5, 2006
By Scott Lowe
Creating users can be tedious, especially when user information already resides in your company’s HR system.
Instead of manually creating a user and assigning passwords, you can use a script that accomplishes the same
goal. The benefits: Fewer iterations of the information will result in fewer errors; you don’t need to worry about
whether or not a user account gets created; and, you can focus on more strategic IT tasks.
This script will create users with passwords in Active Directory in the Users container. You must make a few
minor modifications, explained below, for the script to work in your environment.
Before you begin writing Windows scripts, create a file called schema.ini and place it in your scripts folder. The
contents of schema.ini should be:
[example.csv]
Format=CSVDelimited
ColNameHeader=False
Why do you need this? If your CSV file doesn’t have a header row, some versions of (Microsoft Data Access
Components) MDAC have problems processing the first row of data in your CSV extract file. Basically, your script
may complete ignore the first row of information. If you do use a header row (which I do not for these examples),
you don’t need to create this file.
1. Change the value of the sDomain variable to match the name of your Active Directory environment. I've
used example.com for my sample script.
2. Change the value of the sCSVFileLocation variable. This is the directory in which you will store a CSV
file. Be sure to keep the trailing slash at the end of the variable.
3. Change the value of the sCSVFile variable. This is the name of your CSV file. I've used "example.csv" in
my script.
You can use this script manually by executing cscript createusersfromcsv.vbs from the command line. No
command line parameters are required since everything is handled from within the script. You could also
schedule this script to be run on a periodic basis.
Page 1
Copyright ©2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
Create users in Active Directory from a CSV file with this VBScript
' ---------------------------------------------------
' Script: createusersfromcsv.vbs
' Author: Scott Lowe
' Input: CSV file with layout logonname,firstname,lastname,password
' Date: December 21, 2005
' Change log:
' no changes
'----------------------------------------------------
Option Explicit
Dim sCSVFileLocation
Dim sCSVFile
Dim oConnection
Dim oRecordSet
Dim oNewUser
' Commands used to open the CSV file and select all of the records
set oConnection = createobject("adodb.connection")
set oRecordSet = createobject("adodb.recordset")
oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended
Properties=""text;HDR=NO;FMT=Delimited"""
oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection
' Allows processing to continue even if an error occurs (i.e. dup user)
' We put this below the CSV and AD information since processing can
' continue with a single bad record, but not if there is a problem with
' the CSV file or AD connection
Page 2
Copyright ©2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
Create users in Active Directory from a CSV file with this VBScript
do until oRecordSet.EOF ' Reads the values (cells) in the sInputFile file.
oNewUser.put "sAMAccountName",lcase(sLogon)
oNewUser.put "givenName",sFirstName
oNewUser.put "sn",sLastName
oNewUser.put "UserPrincipalName",lcase(SLogon)&"@"&sDomain
oNewUser.put "DisplayName",sDisplayName
oNewUser.put "name",lcase(sLogon)
Page 3
Copyright ©2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
Create users in Active Directory from a CSV file with this VBScript
Additional resources
• TechRepublic's Downloads RSS Feed
• Sign up for TechRepublic's Downloads Weekly Update newsletter
• Sign up for TechRepublic's Network Administration NetNote
• Check out all of TechRepublic's free newsletters
• Add Global Groups to computers with VBScript
• Two more member-submitted scripts that automate network admin activities
• Automate mundane network admin tasks with four member-submitted scripts
• Three member-submitted admin scripts that will simplify your daily tasks
• Toggle Windows XP Pro IP address settings with this VB script
• Windows XP Pro File Sharing Interface Switcher VB script
• Quickly grab Event Log information in Windows with this script
Version history
Version: 1.0
Published: January 5, 2006
Thanks!
Page 4
Copyright ©2006 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html