VBScript Fundamentals for Windows Scripting – The Basics

This article explains the general basics behind VBScript. Its purpose is to de-mystify VBScript for System Administrators, System Engineers, and anyone else who is interested in writing Windows Script but does not have a background in VB coding.

For many administrators, the thought of writing code throws up an immediate red flag. “That’s not the sort of thing we do, that’s for the developers who write all that gibberish”. The simple fact is that scripting can be as hard or as easy as you choose to make it. Can you write a long and involved script to map drives automatically? Sure. Do you have to? No, it can be as simple as two lines:

Set oDrives = CreateObject(“WScript.Network”)
oDrives.MapNetworkDrive “Z:”, \\SVR01\Home

We’ll go into more detail surrounding the above a little later in the article; the purpose here is to demonstrate that scripting can automate repetitive tasks. How many times have you mapped drives from the command prompt or written them in batch files? Write some script, save some time.

A little VBScript History

When VBScript first came out, it was part of the Microsoft Windows Script Technologies release. VBScript and Jscript (Java Script) were both released to facilitate client-side scripting in web pages. The initial target audience was obviously web developers so system administrators saw no need to get involved since it was used in Web Development. In addition, most of the information surrounding VBScript’s applications in technologies such as WMI (Windows Management Instrumentation) and ADSI (Active Directory Services Interface) was only available to developers in the SDK’s (Software Development Kit) and other targeted information. Lastly, when administrators were able to find some information on the technologies, it was written in pure coding jargon which was not discernable unless you had a developer background.

Today; however, the information on VBScript and scripting technologies is widely available and administrators are noticing the usefulness of the scripts to make their jobs easier.

VBScript Fundamentals

VBScript works with objects (specifically Automation objects) which are simply a smaller set of COM (Component Object Model). COM is what facilitates the functionality of items like .dll’s and .exe’s. Take our earlier example:

Set oDrives = CreateObject(“WScript.Network”)
oDrives.MapNetworkDrive “Z:”, \\SVR01\Home

In this example, we are using the CreateObject method to bind to the element (or object), WScript.Network, and give it the name of “oDrives” for use in the next line. Binding is the method used to make a connection to an Automation object (WScript.Network) and create a new instance of that object (oDrives). We now call the “MapNetworkDrive” method on our oDrives object followed by the arguments directing the method to map the Home share on SVR01 to the Z:\ drive.

Note – When I first learned this stuff, it was right about here that I thought, “You’ve got to be kidding! A few paragraphs ago, the author said this wasn’t that hard”. Keep reading through all the articles including the upcoming ones. These are new concepts for system administrators similar to jumping from NT4 to Active Directory and we all made that leap okay. It will make sense after a little practice, I promise.


The following are a few definitions used in VBScript:

Methods –Functions that an object can perform. In the previous example, we used the CreateObject method to bind to the WScript.Network object.

Variables –Places in memory to store data. Note this example:

Comp = InputBox("Enter the Computer Name", "Computer Name")

In this example we are calling up an input box (this prompts the user to input data), giving it a description for the user which will read “Enter the Computer Name” above the input box, and assigning “Computer Name” to the title of the input window when it appears. All of this information is held in memory and stored as a value for the variable Comp. Unless this information is released from memory later in the script, anytime the variable Comp is called, the value that was entered by the user will be used. Variables can be given any descriptive name as long as it is not reserved for use as a scripting component such as WScript, Const, Set, etc.

Constants -Similar to Variables in the regard that they store data in memory; however, a constant must be defined within the script. In addition, the values cannot be changed within the script once they are defined and will maintain their values throughout the script. See the following example script:

Const FIRST_NAME = "Alan"
Const LAST_NAME = "Finn"
Wscript.Echo "My name is ", FIRST_NAME, LAST_NAME

Above, we are declaring two constants; FIRST_NAME and LAST_NAME and assigning them the values of Alan and Finn respectively. The third line uses the Echo method of the WScript object to print the string “My name is “, then the first constant value and lastly the second constant value in a pop up window. Copy and paste the script into notepad and change the constants to match your name; then save the file as MyFirst.vbs and double click on it to run.

Operators and Control Flow Statements – Operators are used to compare expressions. Basically, it’s logical math comparisons used in VBScript. As an example, let’s say that x = 1 and y = 2. An operator would compare the two and return a response or do something else based on the results such as:

If x > y then this statement is False
Else this statement is True.

Obviously 1 is not greater than 2 so the statement returns as True (even though 2 is greater than 1 because of the way the logic operator was used). As all things in VBScript, this can be as complex or as simple as you make it depending on what your needs are. In addition to the > (greater than) operator used, the above statement also uses a simple flow control example. Since the first line is false, the statement will “flow” into the second line which “controls” the behavior of the statements. Now, let’s put this together into some real VBScript:

1. x = True 'assign variable x a value of true
2. count = 0 'assign variable count a value of 0
4. Do 'outer loop
5. Do While count < 10 'inner loop 6. count = count + 1 'increment the count by 1 7. Wscript.Echo count 'display current count on screen 8. If count = 10 Then 'if this condition is True 9. x = False 'set this value to False 10. Exit Do 'exit the inner loop 11. End If 'ending the If statement 12. Loop 'first loop statement for inner loop 13. Loop Until x = False 'exiting the outer loop

The lines have been numbered for ease of explanation. The “ ‘ “ sign is used to specify comments which are ignored while the script runs. Comments have been added to the right of each line to explain functionality. Okay, let’s break it down by line as follows:

1. Assign the variable called “x” a value of True
2. Assign the variable called “count” a value of 0.
3. Space inserted to separate the variable declaration statements from the script function. Simply makes the script easier to read and is not required.
4. Used the “Do” statement to start a loop. This type of loop (flow control) will continue until a condition becomes either True or False depending on what is specified.
5. Defining the conditional constraints of the loop statement. DO the loop WHILE the COUNT variable is LESS THAN 10. Remember, we assigned the COUNT variable a value of 0 earlier.
6. Incrementing the count variable by 1 each time the statement loops. The first few loops could be considered as COUNT = 0+1, COUNT =1+1, COUNT = 2+1, etc.
7. This displays the current count number in a pop-up window on your screen.
8. The conditional statement. IF COUNT equals 10 THEN do what is on the next line, otherwise go to the LOOP statement and loop back to the DO statement. Until the COUNT variable meets this condition, the statement will continue to loop and increment itself.
9. Assigning a new value to variable X. Once the conditional statement in line 8 is met, change the value of X to FALSE.
10. Since the condition has now been met, we exit the second, or inner, loop.
11. This statement is required to show the cessation of the IF loop in line 8. This is required for the script to know where and when to stop the flow control loop statement.
12. This is the closing statement for the Do loop in line 5. Also required to tell the script when to stop the flow control loop statement from the inner Do loop.
13. This statement closes the outer, or first, Do loop once the condition is met. By this time, the COUNT variable has been incremented to a value of 10 so the variable X has been changed to FALSE meeting the criteria of this condition, therefore the outer loop is closed and the script is complete.

Arrays – When you need to carry out administrative tasks multiple times, arrays offer a method to store multiple values. The Array function is used to assign a set of values to a variable. The following example assigns three colors to the array variable named Colors:

Colors = Array(“red”, ”white”, ”blue”)

It is also possible to create an array variable without using the actual Array function statement by declaring the variable at the beginning of your script using the Dim statement. Declaring a variable simply tells the script that the name you are declaring will be used as a variable within the script. For example:

Dim Colors(2)

This statement tells the script that we are declaring a variable named Colors as an array and that it will contain three values. Yes, I said three values. The first item in every array is actually designated as item 0 which must be taken into consideration when calculating the total amount of objects in the array. In this case, we have three colors so starting at zero we count 0, 1, 2 or simply subtract one from the total values to be used. 3-1=2.

Continuing on, we now will assign values to our array variable named Color starting with item 0:

Dim Colors(2)
Colors(0) = “red”
Colors(1) = “white”
Colors(2) = “blue”

Note – If you tried to assign a fourth variable without increasing the constraints of the array, your script would return an error stating “Subscript out of Range”.

Obviously, this would take a while to create for larger arrays and the goal of scripting is to simplify and automate your job tasks so now we’ll take this one step further and introduce Dynamic Arrays.

Dynamic Arrays are more useful that Arrays in the fact that they don’t require you to specify the number of values and the size of the array can change throughout the script. This next script will read all of the computers in the domain and write the results to a textfile:

1. Dim Container
2. Dim ContainerName
3. Dim Computer
4. Dim fso
5. Dim outfile
7. ContainerName = "INTERNAL"
8. Set fso = CreateObject("Scripting.FileSystemObject")
9. Set outfile = fso.OpenTextFile("c:\DomainPC.txt", 2, True)
10. Set Container = GetObject("WinNT://" & ContainerName)
11. Container.Filter = Array("Computer")
12. For Each Computer In Container
13. outfile.writeline Computer.Name
14. Next

As we did earlier, let’s break it down line by line and review the script:

1. Declare the Container variable.
2. Declare the ContainerName variable.
3. Declare the Computer variable.
4. Declare the fso variable.
5. Declare the outfile variable.
6. Space for separation of declarations from the script function.
7. Assign the string value of INTERNAL (the domain being queried) to the variable ContainerName.
8. Use the CreateObject method to bind the Scripting.FileSystemObject object as a value for the fso variable. This means that fsois now an object for use within the script.
9. Use the OpenTextFile property of the new fso object to create a text file in the C:\ directory named DomainPC.txt and bind it to the outfile variable. Regarding the parameters at the end of the statement; the 2 means the file may be overwritten, and the True designates a Unicode file (false would create an Ascii file).
10. Use the GetObject method to search the domain INTERNAL for computers with accounts in that domain and assign them as values for the Container variable. Note – The WinNT:// parameter is case sensitive and is used in both NT and AD domains. In pure AD domains, it is preferable to use the LDAP:// designation with the LDAP path which we will go into more detail in a future article covering ADSI.
11. Here is where we declare the Dynamic Array. We now use the Filter method on the Container variable to place the output in the dynamic array we are naming as Computer.
12. Beginning of a For… Next loop for flow control. In this line we are telling the script that every value in the array variable Computer will be assigned as a value in the variable Container. We cannot simply use Container for further methods as it is already in use as a variable in the dynamic array so we must create another variable to use. The loop would read as For Each Computer in the array Container, do whatever follows in the script until Next is reached and there are no more Computer values left in the array.
13. Use the writeline property on the outfile object and print the Name property of the Computer object on each line.
14. Go to the Next Computer in the Container array until all array values have been written, then stop the loop.


If you are completely new to the world of VBScript, this is probably a bit overwhelming. The object (no pun intended) of this article is to introduce the main concepts of VBScript. There is entirely too much material pertaining to VBScript to cover in this article. Some portions of the script make sense because they are logical and other parts are foreign and confusing. The syntax of the language will become easier with practice so don’t get discouraged after reading this article. Take some time to analyze the scripts and their explanations and understand what is occurring on each line of the script. I guarantee that any administrator or engineer who writes VBScript (myself included) has a book or two that lists all the syntax and possible parameters available on their desk to refer to. A good starter site for reference is


Also, take a look at some of the following links to get a deeper initial understanding of the VBScript framework:





There are many, many more resources on the Internet as well as books available to review for more information. The above are only a few locations of information.

VBScript isn’t something that will make sense overnight, but the little bit of work required to get familiar with it will pay off tenfold and then some in your environment.

My next article will look at ADSI (Active Directory Service Interfaces) and how VBScript can help you administer directories.