Importing users into AD using VBS Script and CSV File

By scott_langshaw2 ·
Hey guys

Im new on here. Im having a few problems with this script it reads the csv file fine but only reads the first user and then stops and doesnt carry on reading the csv file.

Any ideas here is the script:

Dim sCSVFileLocation, sCSVFile, oConnection, oRecordSet, oNewUser, oRootLDAP, oContainer, sLogon, sFirstName, sLastName
Dim sDisplayName, sPassword, nPwdLastSet, nUserAccountControl, sDomain

sDomain ="swdomain.local"

sCSVFileLocation = "C
sCSVFile = sCSVFileLocation&"year6.csv"

set oConnection = createobject("adodb.connection")
set oRecordSet = createobject("adodb.recordset") "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended Properties=""text;HDR=NO;FMT=Delimited""" "SELECT * FROM " & sCSVFile ,oConnection

Set oRootLDAP = GetObject("LDAP://rootDSE")
Set oContainer = GetObject("LDAP://cn=Users," & _

On Error Resume Next

do until oRecordSet.EOF

sLogon = oRecordSet.Fields.Item(0).value
sFirstName = oRecordSet.Fields.Item(1).value
sLastName = oRecordSet.Fields.Item(2).value
sDisplayName = sLastName&", "&sFirstName
sPassword = oRecordSet.Fields.Item(3).value

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)


oNewUser.SetPassword sPassword
oNewUser.Put "pwdLastSet", 0

oNewUser.Put "userAccountControl", 512


Any help appreciated

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

TR link

by unhappyuser In reply to Importing users into AD u ...

Check out this TR link.


Collapse -

Would that they were all this easy

by neilb@uk In reply to Importing users into AD u ...

You need to step through your RecordSet.

At the moment, you're just sitting in the loop at the first record. Use the oRecordset.MoveNext method just above the Loop statement.


Collapse -


by scott_langshaw2 In reply to Would that they were all ...

Thanks Neil it has worked a treat thanks for your help


Related Discussions

Related Forums