Annoyances.org
Home » Using the Windows Script Host Search | Help | Home
  
Using the Windows Script Host

For many people, education in computer programming ended abruptly in the seventh grade with a class in the BASIC programming language. (Those who continued ended up with pocket protectors, tape on their glasses, and job offers at companies like Microsoft.) However, if you want to take advantage of the new scripting language included in Windows 98 and Windows NT 5.0, and you recognize code like this:

10 print "hello"
20 goto 10

your education is about to get a jump-start. I'll show you how to start writing simple scripts in VBScript for use with the new Scripting Host found in Win 98 and Win NT 5.0.

Jump to:

Using the Windows Script Host
The cool thing about the Windows Script Host is that it is language-independent, meaning that it will work with any modern scripting language. It has built-in support for JavaScript and VBScript, but can be extended (with third-party add-ons) to use almost any other language, such as Perl, TCL, Rexx, and Python. I'll demonstrate scripting using VBScript (based loosely on the Visual Basic language) here, because it's easy to learn and VB is widely known. However, it is a "Scripting" language rather than a programming language, which not only implies that the resulting scripts are much simpler and smaller than full-blown applications, but that the language is much simpler as well.

VBScript files are just plain text files with the .VBS extension, and can be edited with any text editor, such as Notepad. To run a script, just double-click on the script file icon - you'll probably never need to run the Scripting Host program (wscript.exe) directly. You can quickly open an existing script file for editing by right-clicking on it and selecting Edit.

Scripts are built by typing commands, one on each line. Commands are used to either set a specific property of an object to some value, or to carry out some action. Much of VBScript involves referencing objects and setting properties. Most scripts will require the following line:

Set WshShell = WScript.CreateObject("WScript.Shell")

which creates the WshShell object. The various properties and methods of this object are used to accomplish many tasks in VBScript, such as displaying message boxes and retrieving system information. The following excerpt of code shows several examples.

Set WshShell = WScript.CreateObject("WScript.Shell")
age = InputBox("Please type your age.")
newage = age + 5
WshShell.Popup "In 5 years, you will be " & newage & "."

The first line creates our WshShell object for the Popup later in the script. The second line does two things: it first asks the user to type something, and then puts the typed text into the variable age. The third line then creates a new variable, newage, and assigns it to the user's input and adds five (note the lack of any error checking: if the user does not enter a number, this statement will cause an error). The fourth line then concatenates a text string to our adjusted variable, and displays the result in a message box.

Using this simple syntax, we can create more useful scripts that launch and control applications, communicate with the network, and make changes to the registry.

Here's an example of a more complex script. Let's say you use a portable computer that is sometimes connected to a local-area network. While the process of mapping and disconnecting a network drive is not difficult, it can be tedious to do repeatedly. Here's a script that lets you map and un-map a network drive with just one click:

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set AllDrives = WshNetwork.EnumNetworkDrives()

DriveLetter = "N:" 'must be capitalized
RemotePath = "\\dave\c"

AlreadyConnected = False
For i = 0 To AllDrives.Count - 1 Step 2
If AllDrives.Item(i) = DriveLetter Then AlreadyConnected = True
Next

If AlreadyConnected = False then
WShNetwork.MapNetworkDrive DriveLetter, RemotePath
WshShell.PopUp "Drive " & DriveLetter & " connected successfully."

Else
WShNetwork.RemoveNetworkDrive DriveLetter
WshShell.PopUp "Drive " & DriveLetter & " disconnected."
End if

This script requires no user-interaction once it has been executed, and displays only a single confirmation message when it's done. The first three lines initialize our various objects and list all currently mapped network drives. The next two set our custom variables: you'll want to change these.

The next section uses a For...Next loop to go through the list of mapped drives looking for n:; if it's listed, the AlreadyConnected variable is set to True for later use. Finally, an If structure is used to connect or disconnect our network drive, depending on the value of the AlreadyConnected variable.

The WSH also includes cscript.exe, a command-line variant of wscript.exe, which makes it a great replacement for DOS batch files. You can also launch DOS batch files from your scripts to complete even more tasks.

The command-line application of VBScript is quite appealing. For example, you can use VBScript or JavaScript to create simple web server scripts (a.k.a. CGI). Here's a simple script that prints CGI-compatible output

Wscript.Echo "Content-type=text/html "
Wscript.Echo
Wscript.Echo "The current time on this server is: " & Now()

To run this script, type cscript.exe //nologo filename.vbs, where filename.vbs is the filename of your script. You can also associate the .VBS extension with cscript.exe (click View, Options, File Types in Explorer) to run it in command-line mode every time.

Helpful tips
If you're thinking that the Windows Script Host (WSH) would be great for distributing if it was only supported by Windows 95 or Windows NT 4.0, you'll be glad to hear that Microsoft has made it available for free download (it's included with all newer versions of Windows). Just go to Microsoft's web site and download wsh.exe; sample scripts and other information is available there as well.

If you're an administrator for an office full of systems running Win 98 or Win 95 with the WSH installed, you can write and distribute scripts to easily install software, make global registry changes, or put new shortcuts on all your users' desktops.

Using scripts in conjunction with the Scheduled Tasks folder allows you to schedule more than just the launching of a program. Write a simple script to, say, ask before running Scandisk at 5pm every day, or display an anniversary reminder to avoid a night on the couch.

Want to know more?
Both Windows 98 Annoyances, the book, and Windows Me Annoyances, the book, contain entire chapters devoted to the Windows Script Host. Find out how to access the registry, connect and disconnect from a network, manipulate files, create shortcuts with the Windows Script Host, and more!

Additional Resources
VBScript's roots are in Visual Basic (VB) and Visual Basic for Applications (VBA), which means that these two environments are good places to learn about VB. While VBScript, VB, and VBA aren't identical, their documentation is a good place to start learning about methods, objects, variables, and some syntax.

If you have Microsoft Office 95/97/2000, you can use the included VBA macro language help documentation as a partial technical reference for VBScript. If you don't have it installed, run Office setup and choose "Help for Visual Basic." If you don't have Office, you can download the Visual Basic Control Creation Edition 5.0 for free from Microsoft's web site - not only is the documentation somewhat relevant to VBScript, but it's a good environment to expand your programming beyond VBScript.

Be aware, however, that some VB statements and syntax will differ in VBScript. For example, VB's x=shell("cmdline") method isn't supported; instead, use x=WshShell.Run("cmdline").

Also built-in to the Windows Script Host is support for JavaScript. You'll find far more documentation and examples of JavaScript than VBScript on the web and in books, due to it's popularity (JavaScript is supported by both Netscape and Internet Explorer, while VBScript is only supported by IE).

Return Home

All content at Annoyances.org is Copyright © 1995-2009 Creative Elementtm All rights reserved.
Please do not plagiarize; redistributing these pages without permission is strictly prohibited.