Description
This article contains VBScript code to create user in Active Directory with various methods and it also contains VBScript code to Create Bulk AD users from CSV file.
Related Articles: Create AD User Powershell Script , Powershell Script to Create Bulk AD Users from CSV
Summary
1. Create Active Directory user by VB Script.
2. Create Active Directory user in VB Script by user inputs.
3. Create Bulk AD users using VB Script.
4. Create Bulk AD users from CSV file using VB Script.
Create Active Directory user by VB Script
1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Change the value for strUserName if you want to give your own name otherwise simply leave it.
3. Save the file with a .vbs extension, for example: CreateADUser.vbs
4. Double-click the vb script file (or Run this file from command window) to create AD user.
Note: You should run this VBScript on a machine with windows Active Directory domain.
Click to get VBScript code as file Download CreateADUser.vbs
' CreateADUser.vbs ' Sample VBScript to create a User in Active Directory . ' Author: https://www.morgantechspace.com/ ' ------------------------------------------------------' Option Explicit Dim strUserName Dim objRootLDAP Dim objContainer Dim objNewUser strUserName = "MorganTestUser" Set objRootLDAP = GetObject("LDAP://rootDSE") ' You can give your own OU like LDAP://OU=TestOU instead of LDAP://CN=Users Set objContainer = GetObject("LDAP://CN=Users," & _ objRootLDAP.Get("defaultNamingContext")) Set objNewUser = objContainer.Create("User", "cn=" & strUserName) objNewUser.Put "sAMAccountName", strUserName objNewUser.Put "givenName", "Morgan" objNewUser.Put "sn", "TestUser" objNewUser.Put "displayName", "Morgan TestUser" objNewUser.Put "Description", "AD User created by VB Script" objNewUser.SetInfo objNewUser.SetPassword "MyPassword123" objNewUser.Put "PasswordExpired", CLng(1) objNewUser.AccountDisabled = FALSE MsgBox ("New Active Directory User created successfully by using VB Script...") WScript.Quit
Create Active Directory user in VB Script by user inputs
1. Copy the below example VBScript code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: CreateADUserbyUserInputs.vbs
3. Double-click the VBScript file or Run this file from command window
4. Then give the user name in the opened username text box and click OK to proceed…
Note: You can give your own OU like LDAP://OU=TestOU instead of LDAP://CN=Users
Click to get VBScript code as file Download CreateADUserByUserInputs.vbs
' CreateADUserWithUserInputs.vbs ' Sample VBScript to create a User in Active Directory by User Inputs. ' Author: https://www.morgantechspace.com/ ' ------------------------------------------------------' Option Explicit Dim strUserName Dim objRootLDAP Dim objContainer Dim objNewUser Do strUserName = InputBox ("Please enter user name") If strUserName = "" then Msgbox "No user name entered" end if Loop Until strUserName <> "" MsgBox "Please click OK to continue..." Set objRootLDAP = GetObject("LDAP://rootDSE") ' You can give your own OU like LDAP://OU=TestOU instead of LDAP://CN=Users Set objContainer = GetObject("LDAP://CN=Users," & _ objRootLDAP.Get("defaultNamingContext")) Set objNewUser = objContainer.Create("User", "cn=" & strUserName) objNewUser.Put "sAMAccountName", strUserName objNewUser.Put "Description", "AD User created by VB Script" objNewUser.SetInfo objNewUser.SetPassword "MyPassword123" objNewUser.Put "PasswordExpired", CLng(1) objNewUser.AccountDisabled = FALSE MsgBox ("New Active Directory User created successfully by using VB Script...") WScript.Quit
Create Bulk AD users using VB Script
1. Copy the below example VBScript code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: CreateBulkADUsers.vbs
3. You can change the value (For i 0 to 5) to any value like (For i 0 to 100) to create 100 users.
4. Double-click the vb script file (or Run this file from command window) to create Active Directory user.
Click to get VBScript code as file Download CreateBulkADUsers.vbs
' CreateBulkADUsers.vbs ' Sample VBScript to create a Bulk of AD Users. ' Author: https://www.morgantechspace.com/ ' ------------------------------------------------------' Option Explicit Dim strUserName Dim objRootLDAP Dim objContainer Dim objNewUser Dim i For i = 0 To 5 strUserName = "MorganTestUser"& i Set objRootLDAP = GetObject("LDAP://rootDSE") ' You can give your own OU like LDAP://OU=TestOU instead of LDAP://CN=Users Set objContainer = GetObject("LDAP://CN=Users," & _ objRootLDAP.Get("defaultNamingContext")) Set objNewUser = objContainer.Create("User", "cn=" & strUserName) objNewUser.Put "sAMAccountName", strUserName objNewUser.Put "Description", "Bulk AD User created by VB Script" objNewUser.SetInfo objNewUser.SetPassword "MyPassword123" objNewUser.Put "PasswordExpired", CLng(1) objNewUser.AccountDisabled = FALSE Next MsgBox (i &" AD Users created successfully by using VB Script...") WScript.Quit
Create Bulk AD users from CSV File using VB Script
1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: CreateBulkADUsers.vbs
3. Change the CSV file path C:UsersAdministratorDesktopNewUsers.csv with your own file path.
4. Change the domain name workdomain.local to your own domain name.
Note:Your CSV file should contain the columns samAccount, givenName and sn as first three columns
5. Double-click the vb script file (or Run this file from command window) to create Bulk Active Directory users from CSV file.
Click to get VBScript code as file Download CreateBulkADUsersFromCSVFile.vbs
' CreateBulkADUsersFromCSVFile.vbs ' Sample VBScript to create a AD Users from CSV file . ' Author: https://www.morgantechspace.com/ ' ------------------------------------------------------' Option Explicit ' Variables needed for LDAP connection Dim objRootLDAP Dim objContainer ' Variables needed for CSV File Information Dim varFileName Dim objFSO Dim objFile ' Holding variables for user information import from CSV file Dim varSamAccountName,varFirstName,varLastName Dim newUserFields Dim objNewUser Dim varDomain Const ForReading = 1 ' Modify this name to match your company's AD domain varDomain="workdomain.local" ' Create a connection to the Active Directory Users container. Set objRootLDAP = GetObject("LDAP://rootDSE") ' You can give your own OU like LDAP://OU=TestOU instead of LDAP://cn=Users Set objContainer = GetObject("LDAP://cn=Users," & objRootLDAP.Get("defaultNamingContext")) ' Specify the csv file full path. varFileName = "C:\UsersAdministratorDesktopNewUsers.csv" ' Open the file for reading. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(varFileName, ForReading) ' Read the first line - csv columns -not needed for our proceess objFile.ReadLine ' Skip the error while creating new user...(i.e- user already exists) on error resume next ' Read the file and create new user. Do Until objFile.AtEndOfStream ' Splits prioperty values. newUserFields = Split(objFile.ReadLine,",") varSamAccountName = newUserFields(0) varFirstName = newUserFields(1) varLastName = newUserFields(2) ' Create new User account Set objNewUser = objContainer.Create("User","cn="&varFirstName&" "&varLastName) objNewUser.put "sAMAccountName",lcase(varSamAccountName) objNewUser.put "givenName",varFirstName objNewUser.put "sn",varLastName objNewUser.put "UserPrincipalName",lcase(varSamAccountName)&"@"&varDomain objNewUser.put "DisplayName",varFirstName&" "&varLastName objNewUser.put "name",lcase(varSamAccountName) objNewUser.put "description","This user was created from csv file using vbscript" objNewUser.SetInfo objNewUser.Put "pwdLastSet", 0 ' Enable the user account objNewUser.AccountDisabled = FALSE objNewUser.SetInfo Loop MsgBox("Active Directory users created successfully from CSV file using VBScript.") WScript.Quit
It works perfectly ! Thanks !