By David Wiseman (Administrator)published 06 Feb 2008, modified 02 Mar 2009
My Rating:
Vote
Rating:
(1 votes)
Views:31493

VBScript (WSH) Introduction

What is VBScript?

VBScript started out as a client-side scripting language for web pages.  Microsoft later introduced Windows Script Host (WSH) to allow VBScript to be used outside internet explorer.  VBScript is now very popular among system administrators to automate various tasks.

How do I create a VBScript?

VBScript files are text files saved with a "*.VBS" file extension.  This means that you can create VBScript files using Notepad.  When you save a VBScript in notepad be sure to enclose the name in double quotes. e.g. "MyScript.vbs".  The double quotes will ensure that the file gets saved with the right extension (instead of MyScript.vbs.txt).

Although you can use notepad to develop VBScripts, programs like NotePad++ can help with VBScript development.  NotePad++ can highlight syntax and displays the line number, both of which can be useful when developing VBScripts.

How do I run a VBScript?

Example Script:
 

wscript.echo "Hello World!"

 

 VBScript supports two modes of execution - CSCRIPT and WSCRIPT. To run a script in WSCRIPT mode you can simply double-click the script file. Using the script example above, you would see the following output:

 

VBScript files often have "Wscript.Echo" statements to provide information to the user.  If a script has many "Wscript.Echo" statements or they are called in a loop, you might want to consider running the script in CSCRIPT mode.  To run a script in CSCRIPT mode, open a command console.  Change the directory to the folder where your script is located.  Type CSCRIPT "MyScript.vbs"

The output will look similar to the image below.  No confirmation is required.

 

You can also have the output directed to a text file when running in CSCRIPT mode:

CSCRIPT "MyScript.vbs" >> "scriptlog.txt"

VBScript Language

The VBScript language is very similar to Microsoft Visual Basic which is one of the reasons for its popularity.  It's also similar to VBA (Visual Basic for Applications) which is used to automate Microsoft Office. 

One of the key differences with VBScript is that it is a typeless language.  All variables are created as variants, which can be used to store any type of data.  In many other programming languages, including visual basic and VBA, you would declare a data type before storing a value in the variable. e.g.

DIM i as INTEGER
i = 1

The code above is not valid in VBscript as you would need to omit the "as INTEGER" from the variable declaration.  Another point worth noting is that you are not required to declare variables by default.  You could simply write the line of code "i=1" without declaring the "i" variable. 

I recommend that you use the "OPTION EXPLICT" statement at the top of all your scripts.  This statement will force you to declare all variables - although this might seem like extra work, it could save you loads of time when debugging scripts. e.g.

OPTION EXPLICIT
DIM i
i = 1

Further Reading

This has been a very quick introduction to VBScript.  For more information on VBScript and to learn how to program in VBScript I recommend the "Windows 2000 Scripting Guide".  This book is available FREE online:

Windows 2000 Scripting Guide 

Also, there are many examples of VBScripts on this website to help you get started.  Some of the scripts you will find are ready to use as is, others are very short snippets of code that are designed to be used as part of a larger script. 

Find a Script... 

Find a VBScript 

 




  Post Comment
Order By:  
User Comments
      
Be the first to post a comment!