IT Employment

General discussion


DOWNLOA Create users in Active Directory from a CSV file with VBScript

By Bill Detwiler Editor ·
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 handy, VBScript will create users with passwords in Active Directory in the Users container.

Download and try the script:

Join this ongoing discussion and let us know if this script is helpful and if there's anything we can do to improve the download's format or content.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

Add this line while it's building the account

by brollins In reply to User Discription

oNewUser.put "description","Your User Description"

You could also import it from the CSV file.

Collapse -

Exchange Attributes error

by jenueheightz In reply to DOWNLOAD: Create users in ...

I keep getting "Exchange attributes error : -2147016657", any pointers on what that may be?
I would also like to create within a specific OU via the .csv file, actually nested OU. Is that possible?
And as it's only 362 days until Christmas; a pointer for how to create a home directory as well would be great.
I'm trying to teach myself via Don Jones' "Managing Windows with VBScript and WMI". Any suggestions for books that would be helpful?

Thanks all.

Collapse -

"Exchange attributes error : -2147016657",

by richard.chowdhury In reply to Exchange Attributes error

i am too getting the error 'Exchange attributes error : -2147016657' users are created, but mailboxes are not and neither are passwords.
I managed to get them in to an ou, you just change line 99 from :
Set oContainer = GetObject("LDAP://cn=Users," & _


Set oContainer = GetObject("LDAP://ou=your ou name," & _

Collapse -

Pls could any any one post the final working Script

by bambelela In reply to "Exchange attributes erro ...

I don't mind even if exclude Exchange Attributes.

Collapse -

never mind

by itandweb In reply to Pls could any any one pos ...
Collapse -

This scrip works to creat user, and passwords for win server 2003

by nukwar In reply to Pls could any any one pos ...

Here is the script:
' ---------------------------------------------------
' Script: createusersfromcsv.vbs
' Author: Scott Lowe
' Input: CSV file with layout logonname,firstname,lastname,password
' Date: December 21, 2005
' Change log:
' no changes
' Rev: by: Hector Guerra 10-16-2007
' Help by and rewrite by: Sergio Mion

Option Explicit

Dim sCSVFileLocation
Dim sCSVFile
Dim objFSO
Dim objFile
Dim strLine
Dim strItems
Dim oNewUser

' ----------Variables needed for LDAP connection----------
Dim oRootLDAP
Dim oContainer

' ----------Holding variables for information import from CSV file
Dim sLogon
Dim sFirstName
Dim sLastName
Dim sDisplayName
Dim sPassword
Dim nPwdLastSet
Dim nUserAccountControl ' Used to enable the account
Dim sDomain

' ----------Modify this to match your company's AD domain----------

' ----------Input file location----------
sCSVFileLocation = "C:\Scripts\"
' ----------Full path to input file----------
sCSVFile = sCSVFileLocation&"studentinfo45.csv"

' ----------Commands used to open the CSV file and select all of the records----------
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(sCSVFile, 1)

' ----------Create a connection to the Active Directory Users container.----------
' ----------My OU's are 2-3 period, 4-5 period, 7-8 period. My server name is CAD.----------
Set oContainer = GetObject("LDAP://cad/OU=4-5 Period,dc=Autocadcc6,dc=us")

' ----------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
on error resume next

Do Until objFile.AtEndOfStream ' Reads the values (cells) in the sInputFile file.
' --------- Start creating user account----------
' Read variable information from the CSV file
' and build everything needed to create the account
strLine = objFile.ReadLine
strItems = split(strLine,",")

sLogon = strItems(0)
sFirstName = strItems(1)
sLastName = strItems(2)
sDisplayName = sFirstName&" "&sLastName
sPassword = strItems(3)

' ----------Build the User account----------
Set oNewUser = oContainer.Create("User","cn="&sFirstName&" "&SLastName)

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)

' ----------Write this information into Active Directory so we can----------
' modify the password and enable the user account

' ----------Change the users password----------
oNewUser.SetPassword sPassword
oNewUser.Put "pwdLastSet", -1

' ----------Enable the user account----------
oNewUser.Put "userAccountControl", 512


' ----------Used only for debugging----------
'if err.number = -2147019886 then
' msgbox "User logon " & sLogon & "already exists"
'End If

' --------- End of user account creation----------

Collapse -

help with .local domain

by shovett In reply to This scrip works to creat ...

I have tried the script shown here, but always get an error when running it. The line with the error is Set oContainer = GetObject("LDAP://server1/OU=students,dc=server1,dc=local")

It says a referral was returned from the server 8007202b null.

Our domain controller is server1.delasalle.local

How can I adjust this line to meet our needs? I have tried a few different ways, but always get the error.

Thank you for any help you can give me,

Collapse -

Here help for Local domain

by Mardal In reply to help with .local domain

your domain controller is server1.delasalle.local

you posted ("LDAP://server1/OU=students,dc=server1,dc=local")

It should read:

hope this helps as i am new to scripts myself.

Collapse -

"Exchange attributes error : -2147016657"

by mastr2 In reply to "Exchange attributes erro ...

I'm getting error "Exchange attributes error : -2147016657" has anyone ever resloved this issue?

Collapse -

Two Missing lines at end of user creation loop.

by MickKerr In reply to DOWNLOAD: Create users in ...

This script will work pretty much out of the box if you like although there is a pretty major omission at the end of the "User Creation" loop.

Ensure you insert the following two lines at the end of the script


This will ensure that ADO moves to the next line in the csv file, otherwise the script will continually loop on the first entry in the file because it will create it once and then attempt to continually create the first account because it is not being told to move onto the next record.

Related Discussions

Related Forums