Search Tools Links Login

Create an AD User with VBScript


There are are about one hundred different ways to create an Active Directory user, via one hundred different scripting languages. Here is a way to do it with VBScript.

This script is another short, but to the point demonstration of performing basic Active Directory operations with VBScript.  There is not much to it, but hopefully someone will find this useful in perhaps encompassing it in a function as part of a larger program.

Option Explicit

Dim strUser
Dim objRootLDAP, objContainer, objNewUser

strUser = "TestUser001" 

' Bind to Active Directory, Users container.

Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://cn=Users," & objRootLDAP.Get("defaultNamingContext")) 

' Build the user object

Set objNewUser = objContainer.Create("User", "cn=" & strUser)
objNewUser.Put "sAMAccountName", strUser
objNewUser.SetInfo 

After defining a few variables (you need to get into this habit, by the way), the script first makes a connection to Active Directory, specifically to the Users OU. Note the use of defaultNamingContext in this case. By using this method, we don't need to hardwire the domain in the script, thus making the script more portable.

The last three lines build the object, and send it up to Active Directory.  If the account that the script is running under has the necessary permissions, the user will be created. If the account doesn't have the correct permissions, a nice, descriptive error will be shown, saying as much.

Hope this helps someone.

About this post

Posted: 2017-09-19
By: vb6boy
Viewed: 878 times

Categories

Active Directory

Visual Basic Script (VBS)

Windows

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.