Computer Performance, VBScript

How to Create a Computer Account with VBScript

Tutorial for Creating a Computer Account with VBScript

Here are examples of VBScripts that you can use to create computer accounts. Compared with user accounts, computer accounts are relatively easy to script because they need fewer properties. The only compulsory attributes are sAMAccountName and userAccountControl, there is no need to script sn (surname) or password. I designed these scripts in a Windows 2003 Active Directory domain, but they should work equally well in Windows 2000.

Topics for Creating a Computer Account with VBScript

 ♦

Our Mission and GoalVBScript to create computer accounts in Active Directory

Our goal is to create computer accounts. The first mission is to create new computers in Active Directory's Computers container and our second mission is to create more computers, but in a named OU.

My best work is often getting people started.  Therefore here on this page, we concentrate on the essential VBScript commands, namely, to bind with Active Directory and create the Computer object.  Once we have mastered the basics, I have a separate page to tackle bulk import by reading computer names from a spreadsheet.

Example 1 - Sample Script to Create a Computer in Active Directory

Prerequisites

Recommended: that you logon as administrator, preferably at a 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 Computer Account in Active Directory

  1. You need access to 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 strComputer.
  4. Save the file with a .vbs extension, for example: computer.vbs.
  5. Double click computer.vbs and check the Computers container for strComputer.

Script to Create a Computer in Your Active Directory

 

' Computer .vbs
' Sample VBScript to create a computer in Computers .
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.8 - May 2010
' ------------------------------------------------------'
Option Explicit
Dim strComputer
Dim objRootLDAP, objContainer, objComputer
strComputer = "XPSimple3"

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

' Build the actual computer.
Set objComputer = objContainer.Create("Computer",_
"cn=" & strComputer)
objComputer.Put "sAMAccountName", strComputer & "$"
objComputer.Put "userAccountControl", 4096
objComputer.SetInfo

WScript.Quit

' End of Sample Computer VBScript.

VBScript Tutorial - Learning Points

Note 1:  The header section (first 10 lines) explains the purpose of the script and declares 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 Computers container, as that is where the strComputer will be born.

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

Note 4: sAMAccountName controls the ComputerName.  Computer accounts need a dollar sign appended to their name, see where I add: & "$".

Note 4:  The correct userAccountControl value for a computer is 4096.  (If this were a user account then the value would be 512 or 514.)

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download Solarwinds 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. Download your FREE bulk import tool.

If you need more comprehensive software, download a free trial of SAM (Server & Application Monitor)

Example 2: Sample Script to Create a Computer in a Named OU

Prerequisites

That you already have an OU to hold the new computers, my OU is called Accounts

Instructions for Creating a Computer Account in a Named OU

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

' ComputerOU .vbs
' Sample VBScript to create a computer in Computers .
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - May 2010
' ------------------------------------------------------'
Option Explicit
Dim strComputer, strOU
Dim objRootLDAP, objContainer, objLeaf, objShell

strComputer = "XPSimpleOU"
strOU = "OU=Accounts ," ' Note the comma

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

' Build the actual computer.
Set objLeaf = objContainer.Create("Computer", "cn=" & strComputer)
objLeaf.Put "sAMAccountName", strComputer & "$"
objLeaf.Put "userAccountControl", 4096
objLeaf.SetInfo

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

WScript.Quit

' End of Sample ComputerOU VBScript .

VBScript Tutorial - Learning Points

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

Note 2:  This command looks easy to script: GetObject("LDAP://" & strOU & _.  However it took me ages to get the comma, the speech marks and the ampersands (&) just right.  O.K there is no comma here; because I ended up putting the comma at the end of strOU.

Note 3: objShell.run is just me having a little fun.  What this command does is open the Active Directory Users and Computers MMC ready for you to inspect the new computer account.

Summary for Creating Computer Accounts

In addition to creating the actual computer account, take the time to learn the VBScript object (Computer), method (Create) and value (strComputer) technique.  Using these example scripts, you can soon create a new computer account either in an OU or in the Computers container.

 

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   • VBS PwdLastSet

VBScript create computer   • PowerShell create computer from spreadsheet   • Free Import Users Tool

VBScript change password  • VBScript to create group   • SolarWinds Free WMI Monitor

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to 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-2016 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: