Computer Performance, VBScript

Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



How to Create a User Account with VBScript

Tutorial for Creating a User Account with VBScript

This page has VBScript examples, which show you how to create User accounts in your Windows Server domain.  I urge you to trust me, and build your scripts in stages.  The benefit of creating scripts in small sections is that not only do you understand each part, but also your brain will see ways of applying a section in a different scenario.  For example, once you learn how to bind with Active Directory to create a User object, it's easy to modify the script and create a Computer object instead.

Topics for Creating a User Account with VBScript

 ♣

Our Mission and GoalCreating Users with VBScript

Our first goal is to create a User account in Active Directory's Users container.  A more realistic mission is to create users in a named OU, and we will tackle that goal in the second example.

Example 1 - Script to Create a User in Active Directory

On this page we concentrate on the essential VBscript commands necessary to build a User account in Active Directory Users and Computers.  For example, GetObject("LDAP://rootDSE") and .Create("User").  Even though I am experienced at creating VBScripts, I still run manually through creating the object in Active Directory Users and Computers, the menus actions help me to rehearse the stages in my scripts.

Prerequisites

I recommend that you logon at a Windows Server domain controller.  If you are a long way from the server, Remote Desktop would be a suitable alternative.  If that is not possible, you could get these scripts to work from an XP machine as a non-administrator.  However, why introduce extra complications?  Especially at the beginning, you want easy success, with fewest obstacles.

Instructions for Creating a User Account in Active Directory

  1. You should run this VBScript on a Windows Active Directory domain.
  2. Copy and paste the example script below into notepad or a VBScript editor.
  3. Decide whether to change the value for strUser.  DomGuy2 is not a particularly attractive name.
  4. Save the file with a .vbs extension, for example: Users .vbs.
  5. Double click Users .vbs and check the Users container for strUser.

Script to Create a User in a Named OU (Organizational Unit)

 

' Users .vbs
' Sample VBScript to create a User in Users .
' Author Guy Thomas http://Computerperformance.co.uk/
' Version 1.3 - September 2010
' ------------------------------------------------------'
Option Explicit
Dim strUser
Dim objRootLDAP, objContainer, objNewUser
strUser = "DomGuy2"

' Bind to Active Directory, Users container.
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://cn=Users," & _
objRootLDAP.Get("defaultNamingContext"))

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

WScript.Quit

' End of free sample Create Users VBScript.

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click and import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in the free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

VBScript Tutorial - Learning Points

Note 1:  The first 10 lines explain the purpose of the script and declare the variables.

Note 2:  The simple, but clever command, which allows the script to work with any domain is: GetObject("LDAP://rootDSE").  Crucial, this statement binds WSH / VBScript to Active directory.  The next line puts the focus on the Users container, as that is where the user will be born.  Incidentally, the correct syntax is cn=users, whereas OUs that you create need the OU= prefix, for example OU=Accounts,.

Note 3:  sAMAccountName controls the logon name, this is the name that users should enter in the dialog box after they press the Ctrl Alt Delete, logon sequence.

Note 4:  .Create is a method to build an object.  See how we use "User" not "Computer" or "OU".

Note 5:  When creating or modifying users, invariably you need .put and .SetInfo.   The .put method is the equivalent of selecting a box in Active Directory Uses and Computers, in this example sAMAccountName sets the correct property and .put unloads the value set by strUser.  .SetInfo is the VBScript equivalent of pressing the OK button in the GUI.  In both cases it represents the final act of creating or modifying the User object.

Note 6:  This script represents 'work in progress'.  For a real production script you would need to enable the account, and most likely, add several other properties, for example givenName.  My desire is to get you started.  Build the script in stages, understand each component, then add another section.

Recommended: Solarwinds' Permissions Analyzer - Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like the Permissions Monitor because it enables me to see WHO has permissions to do WHAT at a glance.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, and takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free SolarWinds utility saves when you are troubleshooting authorization problems for user's access to a resource.

Download SolarWinds' Free Permissions Analyser - Active Directory Tool

Example 2: Script to Create a User in a Named OU (Organizational Unit)

Prerequisites

Create a new OU.  I called my OU Accounts, what name will your choose?

Instructions for Creating a User Account in a Named OU

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Find the strContainer, and then change to the name of your OU.
  3. Decide whether to change the value for strUser.
  4. Save the file with a .vbs extension, for example: ComputerOU.vbs.
  5. Double click ComputerOU.vbs and check the Computers container for strComputer.
 

' UserOU.vbs
' Sample VBScript to create a User in a named OU.
' Author Guy Thomas http://Userperformance.co.uk/
' Version 2.4 - September 2010
' ------------------------------------------------------'
Option Explicit
Dim objRootLDAP, objContainer, objUser, objShell
Dim strUser, strName, strContainer

strUser = "BookKeeper21"
strName = "Bookie"
strContainer = "OU=Accounts ," ' Note the comma

' Bind to Active Directory, Users container.
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strContainer & _
objRootLDAP.Get("defaultNamingContext"))

' Build the actual User.
Set objUser = objContainer.Create("User", "cn=" & strUser)
objUser.Put "sAMAccountName", strUser
objUser.Put "givenName", strName
objUser.SetInfo

' Optional section to launch Active Directory Uses and Users
Set objShell=CreateObject("WScript.Shell")
objShell.Run "%systemroot%\system32\dsa.msc"

WScript.Quit

' End of Sample UserOU VBScript.

VBScript Tutorial - Learning Points

Note 1:  The key difference between the two scripts is: strContainer = "OU=Accounts ,".  Trace how VBScript applies this variable to set the Organizational Unit.

Note 2:  This command looks easy to script: GetObject("LDAP://" & strContainer & _.  However it took me ages to get the speech marks and ampersands (&) just right.

Note 3: objShell.run. This optional section is just me having a little fun.  What this section does is open the Active Directory Users and Users MMC ready for you to inspect the new User account.  My other reason for adding this code is show that the script has executed successfully, otherwise I just sit and wonder if it has finished yet.

Note 4: I suggested in Example 1 that you could add other attributes, trace how I added givenName through strName.  To see what I mean, I suggest that you alter the value from "Bookie" to a more realistic name.

Summary of Creating User Accounts

The first example script shows you how to create a new user account in your Windows Server domain.  In the second example we control the name of the OU where the account appears in Active Directory Users and Computers.  The tutorial's hidden agenda is learning how to apply the VBScript object, methods and value technique.  There is one other guiding principle, start simply.  Build complex scripts in stages.

If you like this page then please share it with your friends

 


See more VBScript examples:

VBScript create users   • VBScript create contact   • Create contact Exchange

VBScript create computer   • PowerShell create computer from spreadsheet

VBScript change password   • VBS PwdLastSet  • VBScript to create group

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: