Home > Articles > Programming > Visual Basic

Using VBScript

  • Print
  • + Share This
This chapter is from the book

The Windows Scripting Host

In addition to VBScript's multiple uses on the Internet, Microsoft has also recently touted VBScript as a replacement for batch files. By installing the Windows Scripting Host, available for free at http://www.microsoft.com/scripting, you can run VBScript commands from an MS-DOS Prompt window or Windows Explorer. The VBScript code itself is created in a text editor and stored in a file with a VBS extension.

Running Scripts

The Windows Scripting Host actually installs two hosts that can run VBScript code: CSCRIPT.EXE, which is designed for use in an MS-DOS Prompt window; and WSCRIPT.EXE, which is designed for execution from Windows.

To demonstrate the difference, create a VBS file that uses the WScript.Echo method, as in the following example:

WScript.Echo "Hello, world!"

Executing the preceding statement with CScript causes "Hello, world!" to be printed in the MS-DOS Prompt window. WScript, on the other hand, presents the text in a message box.

The simplest way of executing a script is to supply the script filename as a parameter to either of the preceding executables:

CScript.exe d:\scripts\test.vbs

You can also just double-click a script file in Windows Explorer. However, several command-line options are available for the Scripting Host, as listed in Table 30.5. These options must be preceded by two forward slashes (//) in the command line.

Table 30.5

Scripting Host Parameters



//B and //I

Select batch or interactive mode. Batch mode does not display any prompts.

//logo and //nologo

Control display of Microsoft copyright information.


Determines a script timeout in seconds. The script is stopped if execution time exceeds the timeout value.


Makes CScript the host associated with your script files.


Makes WScript the host associated with your script files.


Saves these options (for the current Windows user only).

You can also place the options in Table 30.5 in a WSH file. A WSH file is like a PIF file for batch files; it contains the options in INI-file format:




Any arguments passed to the script itself are included after the Scripting Host options and script filename. You can retrieve them by using the Arguments collection of the Wscript object:

Dim nCount
nCount = WScript.Arguments.Count
WScript.Echo "There are " & nCount & " arguments."

For i = 0 to nCount - 1
 Wscript.Echo WScript.Arguments(i)

Useful Objects and Methods

The usefulness of any object library depends on the types of objects and methods it exposes to the programmer. Because the Windows Scripting Host is intended to replace or supplement batch files, it includes many useful features for accessing the operating system, including the following:

  • Creating desktop shortcuts

  • Mapping network drives

  • Accessing the Registry

  • Working with automation objects

  • Working with NT 5.0 user accounts

You can download sample code to perform all the tasks in the preceding list, along with complete documentation for the Windows Scripting Host, from the Microsoft Web site. Three main objects let you perform these tasks. You have already seen the WScript object in sample code. The WScript object is available directly from VBScript.


The Windows Scripting Host, along with documentation and examples, can be downloaded from the Microsoft Scripting Technology Web page at http://www.microsoft.com/scripting.

Two other objects, Shell and Network, must be referenced through WScript by using the CreateObject function. For example, the following script uses the Run method of the Shell object to run a program and wait for it to finish:

Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
'first parameter is Window Style, second is Wait
WshShell.Run "notepad",1,True
MsgBox "Done!"

As you work with the Windows Scripting Host, you will see that it contains tremendous power to automate Windows-based tasks.

  • + Share This
  • 🔖 Save To Your Account