VBScript Introduction

VBScript is a proprietary scripting language developed by Microsoft and based on Visual Basic. It was designed and released in the mid-1990s, primarily as a light-weight scripting language for use in Microsoft environments. It was also intended early on as an in-browser language, an alternative to JavaScript.

History

VBScript was developed at Microsoft about the same time as JScript, which was Microsoft's implementation of the JavaScript language. They were both targeted primarily at web developers initially. VBScript, however, because it had an API linked to the host environment (called COM, or Component Object Model), and because it was fairly easy to use, quickly gained usage among Microsoft system administrators.

As VBScript's popularity increased, it found its way into just about every Microsoft environment available. Also, the language expanded, to include regular expressions, object-oriented programming principles, and other advanced features. By version 5 of the language, it was capable of doing just about anything other major languages were capable of.

With the advent of the .NET framework, Microsoft decided to fold VBScript into that framework. Due to its inclusion in .NET, VBScript is nearly universally available in Microsoft environments.

Interestingly, for as much as VBScript has expanded its reach over the last two decades, there is one place where it is no longer available: the browser. VBScript was never available in non-Microsoft browsers. The last version of Internet Explorer to include support for VBScript was version 10. Internet Explorer 11, and the new Microsoft Edge browser, do not support VBScript. (Actually, it is possible to run VBScript in Internet Explorer 11, if you turn on compatibility mode. But this isn't recommended.)

Primary Uses

VBScript is a general-purpose scripting language, and could, in theory, be used for any number of purposes. However, most VB script usage falls into a small handful of categories:

  • Systems administration: administrators of various Microsoft systems were among the first adopters of VBScript, as it was a marked improvement over earlier alternatives. It continues to be used by sysadmins, and its use is somewhat analogous to the use of Bash or Perl among Linux administrators. VBScript is beginning to be displaced by PowerShell in this area, but VBScript continues to be used quite a lot.
  • Testing: VBScript is used in Quick Test Professional (now called Unified Functional Testing), a popular unit testing suite.
  • Embedded devices" VBScript is also relatively popular among developers of embedded applications (user interaction software built into custom purpose devices).
  • Macros and in-app scripting: VBScript also gets a fair amount of use within other Microsoft applications, such as MS Office.

VBScript and Web Development

VBScript was originally targeted at web developers, both for client-side (in-browser) and server-side scripting. With JavaScript's rise to dominance, VBScript's use in the browser was never anything more than marginal. It did get some use for server-side development in the context of Microsoft's Active Server Pages (APS), which could run VBScript scripts via a .dll.

Even though in-browser use has been scant (and is no longer supported), the browser-based features of VBScript can be exploited in the context of HTML Application, a Windows app format that allows a desktop UI to be built in HTML and powered via one or more scripting languages. This approach is fairly popular among Windows sysadmins and developers, as it allows them to get a prototype up and running pretty fast. (This desktop use of HTML dating back to the late 1990s prefigures the rise of modern app engines like Electron , NW.js , and PhoneGap ).

Language and Examples

Below we will provide a very basic introduction to the language.

Features

VBScript is primarily a functional language. It supports classes and objects, but not dogmatically so. Early versions of the language did not support classes and other object-oriented features. Standard logic and control-flow structures are available, as are a wide variety of data types.

The main "unit" of programming is the procedure. There are function procedures and sub procedures. Sub procedures do not return a value, but can perform actions.

Unlike Visual Basic, VB does not have much ability to define a GUI. It can create simple popup alerts, but a form-based application would need something like HTML or XML to define the UI.

Environment

VBScript has to be run in a supported environment. For server-side web development, that environment is ASP. For systems automation, it is usually the Windows Script Host.

Syntax

VBScript was based on Visual Basic, so its syntax is most similar to that. Visual Basic was based on BASIC, and retained many syntax features from that language. If you learned to program in BASIC, VBScript will probably look at least somewhat familiar.

VBScript syntax looks a little bit like JavaScript, but there are enough differences that it can be easy to get tripped up. Similarities include dot-notation for accessing the Component Object Model that looks a lot like accessing the Document Object Model in JavaScript.

Variables should be declared using the dim keyword before they are used. You can specify the variable type, or leave it undefined until a value is assigned.

Dim greeting
Dim recipient As String
Dim message
greeting = "Hello"
recipient = "World!"
message = greeting & " " & recipient
document.write(message)

Unlike JavaScript, variable names are case-insensitive (as is most of the language), and semi-colon terminators are not requires — line breaks serve as statement terminators.

Comments in VBScript are single-line only, and are defined by a leading single apostrophe ( ' ).

' This is a comment.

Functions, procedures, if statements, and other code-blocks are declared with a key word statement and ended with the end statement.

'simple if statement
if x > y then
  wsh.echo "X is Greater"
end if

Resources

Should I Learn VBScript?

If you are an IT professional or Systems Administrator working in a Microsoft environment, you probably should be familiar with VBScript, and its use as a system automation tool. Even though you can use PowerShell and other languages, you will likely encounter older automation and testing scripts written in VBScript, which you will need to maintain, debug, or rewrite.