How to Use VBScript

close up photo of hands typing on laptop

Though you may not realize it, anytime you are using a Windows-based computer, you are working with VBScript.

Even though it is an older scripting language, it is still a popular tool, especially for system administrators.

It permeates the web and is found on pages across the internet. It’s a powerful scripting language for web pages that works on both the server-side or the client-side.

 

We’ll look at how VBScript works, and then discuss the ways you can use it today.

What Is VBScript?

VBScript, or Visual Basic Script, is a scripting language developed by Microsoft. It is based on Visual Basic working as a component-based scripting language. It operates like a simplified version of Visual Basic. With a fast interpreter that works on Microsoft platforms, VBScript utilizes a Component Object Model (COM) to connect with and use features in the system environment.

The COM accesses objects in the environment where it runs. For example, with VBScript you can access files using the FileSystemObject (FSO). This allows users to create, read, delete and update files in the Windows environment.

How Is VBScript Used?

Microsoft Windows system administrators will use VBScript to create tools for managing Windows-based computers. This includes functionality like error-handling, subroutines, and other basic administrator functions.

You can use VBScript to create scripts that run daily reports then deliver them to users across an intranet. Many office tasks that need to be run on a windows computer can be done in VBScript. With the right script, you can automate sorting, organizing, and data entry tasks.

VBScript is also the language used in Microsoft Excel. If you “hack” into an Excel file, you’ll be working in VBScript. Advanced Excel users will write queries and automation scripts for Excel in VBScript.

Since VBScript doesn’t need to be downloaded onto a Windows computer, it is often used to run scripts for automating software testing.

VBScript also works similarly to Javascript when it is used for client-side web development. VBScript is embedded in Internet Explorer (IE), so it can be used to create an executable script in HTML. It can be used to create applets and widgets for web pages.

Please note that not all web browsers are natively compatible with VBScript. Built-in support for VBScript is not included with Firefox and Opera. You’ll need to include cross-browser compatibility or some type of client-side scripting in these browsers. Many developers choose Javascript rather than VBScript.


The History of VBScript

VBScript was originally part of Microsoft Windows Script Technologies. Created for web developers in 1996, it was also used for system administrators who wanted a quick and easy automation tool that didn't require complex and time-consuming coding using programming languages like C++.

Over time, Microsoft continued to expand the functionality and use of VBScript, especially for system administrators. Regular expressions were added, including the With statement and functions like Eval, Execute, and ExecuteGlobal. Statements like these allowed users to execute scripts while another script was being executed.


VBScript and the Move to .NET

When Microsoft adopted the .NET framework, which includes a class library for Microsoft Windows that works with several different programming languages, Microsoft decided to include future support for VBScript as part of ASP.NET.

No new versions of VBScript would be developed. Only bug fixes and security updates would be offered. With Internet Explorer 11, Microsoft stopped support for VBScript, though an emulator was offered to provide support for VBScript in later IE releases.

For system administrators, the primary users of VBScript, Microsoft released Windows Powershell. Powershell is a tool that is like VBScript but with notable differences. It provides automation tools and system configuration functionality much like VBScript. It is not primarily a scripting language, but a shell with functionality that provides access to admin functions. Scripting functionality is more difficult using Powershell.

Many system administrators have been slow to adopt Powershell, citing the difficulty in learning how to script with it and problems with backward compatibility on older systems.

Despite the lack of support, VBScript is still included in all new releases of Microsoft Windows. Many users still turn to VBScript for automation tasks. These users don’t believe new releases will in any way diminish the usefulness and power of VBScript.


Features of VBScript

Let’s look at a few of the features and functionality you will find with VBScript.

1. VBScript Language

Since VBScript is based on Visual Basic, it provides the same language categories. These include:

  • Array Handling: Create and use data arrays.
  • Control Structures: Manage and regulate the flow of a VBScript script execution.
  • Constants: A name in VBScript that replaces a number or string and will not change in the execution of a script.
  • Date and Time Functions: Allows users to store and use Date and Time data for use in scripts.
  • Error Handling: Automates program execution to ensure scripts run error-free, efficiently, and smoothly.
  • Mathematical Functions: Basic math functionality like absolute values, sine, cosine, and tangent, products, and integral values.
  • Objects: Objects are class instances, with classes determining the variables, properties, procedures, and events of an object.
  • Procedures: A set of VBScript statements contained by declaration statements.
  • Regular Expressions: A function that identifies patterns in input text, with the pattern consisting of multiple character literals, operators, or constructs.
  • String Manipulation: VBScript includes several built-in methods for processing strings.
  • User Interaction: VBScript offers methods of interacting with users. This could include input box for collecting data or entering values, or a message box for displaying information.
  • Variables: A memory location that contains a value that can be changed or modified during script execution.

The VBScript has a few other identifying features. The main construct in VBScript is the procedure. This separates code into smaller modules that perform an action.

A function in VBScript delivers a result to a statement. A subroutine does not deliver a result. Parameters in VBScript are positional and can be delivered by value or reference.

Coding in VBScript includes constants. These constants include vbYellow and vbBlack for color values, True and False for logic values, vbOKCancel and vbYesNo for MsgBox codes. VBScript includes many other constants, which aid in the readability and ease of use of the script.


2. Functionality in VBScript

VBScript provides easy access to a number of features and functionality. These include:

  • Network Printers
  • Network Shares
  • Network User Information including groups and membership ID
  • Commonly accessed folders like Desktop and Documents
  • Named and unnamed command line arguments
  • Functionality for runtime execution of text, such as Eval and Execute
  • A method of embedding a VBScript language in another application, allowing error reporting and a debug mode
  • Windows Management Instrumentation (WMI)

Many of the features and functions found in VBScript focus on the Windows system administration capability.

VBScripts ease of use and native capability make it a powerful tool for managing networks, especially for Microsoft users. It allows for easy automation of basic office tasks, such as monitoring computers using Windows. Server administrators also like the automation and monitoring capabilities found in the basic VBScript functionality.


3. Other Functionality in VBScript

VBScript uses Scripting Runtime Library (scrrun.dll) for file system management, modifying files, and streaming text operations. Using the Scripting Runtime Library, you have access to objects like File, FileSystemObject, and Textstream allowing access to the Windows file system.

VBScript also allows functionality using ActiveX. Database access is provided through ActiveX Data Objects (ADO). Security concerns have hurt adoption of ActiveX, but it is still available in limited cases through VBScript.

The “ADODB.Stream” class provides binary file and memory I/O functionality. This provides string builders and interconvert byte arrays and strings.

VBScript is currently the base scripting language for Quick Test Professional, a widely-used software test automation tool. It provides a quick and easy way to write automated test scripts for several applications.

Where to Execute VBScript

VBScript can only be executed in a compatible host environment. This is typically not a problem, as Microsoft Windows offers several native environments. These include:

In addition to these environments, it is possible to embed a VBScript hosting environment in another application, using tools like Microsoft Script Control (msscript.ocx). This is a powerful feature for building web applications.


A Final Word on VBScript

When VBScript moved to ASP.NET support, many felt the scripting language would soon be forgotten. It has continued to be a popular tool, especially for system administrators and software testing professionals.

Even with the move to implement other tools like Powershell in place of VBScript, users continue to rely on this older scripting language. VBScript is much easier to use. It is widely accepted by users and provides more than enough functionality without the complexity and problems of other tools.

For now, there is little reason for users to adopt or implement a different solution when VBScript works so well. Even with no new expected updates coming for VBScript, it will continue to be the go-to solution for many administrators.

What do you think?