fortypoundhead.com

Create an AD User with VBScript

Posted On 2017-09-19 by VB6Boy
Keywords:
Tags: VBScript Active Directory Windows
Views: 64


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 the Author

VB6Boy has posted a total of 22 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:107.20.115.174

Before you can post, you need to prove you are human. If you log in, this test goes away.




Quick Links