Microsoft

Configure IT Quick: Getting started with Windows Script Host

Learn about the features in Windows Script Host and its native scripting engines, VBScript and JScript, that make automating just about any Windows task a snap.


Are you a big batch file advocate? If so, you probably got hooked on them back in the DOS days when you had to do a lot of typing at the command line to accomplish just about any task. Chances are good that you still use and create batch files to perform certain tasks in Windows. After all, batch files are still viable tools in the Windows environment and can still save you time when performing certain operations. In this Daily Drill Down, I’ll introduce you to Windows Script Host and explain how it works. I’ll also provide you with a list of resources that you can use to learn more about Windows Script Host.

Batch file limitations
While batch files are still viable, they’re a bit antiquated when compared to today’s Windows environment. They have a limited and inflexible command structure, lack looping functionality, have poor user-interface features, and must access the slow DOS environment in order to run.

Windows scripting advantages
Considering these limitations, shouldn’t you be using a more up-to-date method of automating tasks in the Windows environment—like Windows Script Host and scripts written in VBScript or JScript? Windows Script Host and its native scripting engines, VBScript and JScript, are literally packed with all kinds of features and capabilities designed to make automating just about any Windows task a snap. From launching applications to simplifying file-management tasks, making changes to the registry, and automating networking operations, Windows Script Host has it all. Furthermore, Windows Script Host is a 32-bit application that runs in the Windows environment.

As an IT professional, you owe it to yourself to begin exploring how the Windows Script Host can help save time and make you more productive. Who knows, once you learn about the Windows Script Host, you may decide to give up batch files forever.

Background
Windows Script Host first made its appearance in Windows 98 as a native scripting environment called Windows Scripting Host. Soon after Windows 98 came out, Microsoft released a Windows Scripting Host add-on for Windows 95 and Windows NT and made it available as a free download.

Unfortunately, Microsoft failed to provide any Windows Scripting Host documentation in the Windows 98 manual or in the Help system. Furthermore, the Windows Scripting Host information on Microsoft’s scripting site was sparse and very cryptic. Therefore, the Windows Scripting Host remained in the shadows and the idea of replacing batch files with scripts never really caught on—until recently, that is.

Over the last couple of years, Microsoft has continued to develop Windows Scripting Host and its VBScript and JScript scripting tools. The scripting environment is now at version 2.0 and has been renamed Windows Script Host. The VBScript and JScript scripting engines have also been updated and are now at version 5.5. These new versions are much more robust than their predecessors and offer many new features and improvements.

Many authors have jumped on the Windows Script Host bandwagon and there are now many books on the subject. Microsoft’s scripting site also now contains a plethora of information on Windows Script Host and the native scripting languages VBScript and JScript. (I’ll summarize these additional learning tools in the Resources section at the end of this Daily Drill Down.)

How does Windows Script Host work?
Because Windows Script Host is a native scripting environment for the Windows operating system, you can write a program in a scripting language such as VBScript or JScript and run it right in Windows, simply by double-clicking on your script file. While this statement accurately reflects what happens on the outside, behind the scenes it’s actually a bit more complicated. The process consists of a set of carefully orchestrated communications between the script and the operating system and involves Windows Script Host and a scripting engine as intermediaries. Let’s take a closer look.

Working from the inside out, Windows Script Host is integrated into the operating system, where it acts as an interpreter between Windows and a scripting engine. The scripting engine acts as an interpreter between Windows Script Host and a script. The job of Windows Script Host is to provide access to the scripting engine of a number of operating system objects, such as files, folders, the registry, and network items. The job of the scripting engine is to provide you with a scripting language that you can use to access and manipulate the various operating system objects provided by Windows Script Host.

Windows Script Host objects
Now that you have a basic understanding of how Windows Script Host works and the job that it performs, let’s take a closer look at the objects that it makes available. In this context, an object is basically an operating system component that provides you with a collection of subroutines (methods) and variables (properties) for working with that object from within a scripting language. Keep this description in mind as we examine some of the objects more closely.

Windows Script Host provides you with access to a number of objects that you can use from within your script to access various components in the operating system. However, there are four basic objects that you’ll use the majority of the time: Wscript, WshShell, WshNetwork, and FileSystemObject.
FileSystemObject is actually provided by Windows Script Host’s native scripting languages, VBScript and JScript. Since those scripting engines are built into Windows Script Host, however, use of the FileSystemObject is transparent.
The Wscript object
The Wscript object plays a central role in the Windows Script Host object model in that it’s always available and provides the capabilities to retrieve information about a running script, create and retrieve objects from other programs, display messages, and exit a script. Examples of the Wscript object’s primary methods and properties are shown in Table A.

Table A
Item Type Name Function
Method CreateObject Allows the creation and manipulation of objects in other programs.
Method Echo Displays message boxes on the screen.
Method Quit Exits the currently running script.
Property ScriptFullName Returns the full path and name of the currently running script.
Property Path Returns the full path to the location of the Windows Script Host program.
Property Version Returns the version number of the Windows Script Host program.
Primary methods and properties for the Wscript object

The WshShell object
The WshShell object is very powerful. It provides you with direct access to the Windows shell and the registry. This allows you to access special folders, create shortcuts, run applications, display dialog boxes, retrieve information on button clicks, and much more. Examples of the WshShell object’s primary methods and properties are shown in Table B.

Table B
Item Type Name Function
Method Popup Displays a popup dialog box.
Method RegWrite Creates a registry key or value.
Property SpecialFolders Provides access to folders such as Desktop or Start menu.
Property Environment Returns the current environment variable.
Primary methods and properties for the WshShell object

The WshNetwork object
The WshNetwork object provides you with the ability to work with network components. For example, you can get information about computers on the network, query the network for available resources, and even map and disconnect drive letters. Some examples of the WshNetwork object’s methods and properties are shown in Table C.

Table C
Item Type Name Function
Method EnumNetworkDrives Displays current network connections.
Method MapNetWorkDrive Maps a remote folder to a local drive letter.
Property ComputerName Returns the current computer’s name.
Property UserName Returns the names of users.
Primary methods and properties for the WshNetwork object

The FileSystemObject
As I mentioned above, the FileSystemObject is actually provided by the Windows Script Host’s native scripting languages, VBScript and JScript. However, it’s one of the most useful objects in the scripting system. The FileSystemObject has only one property, but it has numerous methods that allow you to perform a wide range of file-management tasks. For example, the FileSystemObject allows you to create and delete files/folders, copy and move files/folders, get information on files/folders, and even determine if a file/folder exists. Examples of the FileSystemObject’s primary methods are shown in Table D.

Table D
Name Function
DeleteFile Deletes a file or set of files.
CopyFile Copies a file from one location to another.
GetFileName Returns a filename.
FileExists Tests if a file exists.
OpenTextFile Opens a text file for reading or writing.
Methods for the FileSystemObject

Analyzing the objects
As you can see from the sample list of methods and properties presented in Tables A through D, Windows Script Host provides you with all the tools you need to interact with the Windows operating system. However, its real power comes from the fact that you can combine these objects, methods, and properties with the robust features and command structure provided by the scripting languages to create very complex tools that can give you way more power over the operating system than you could ever imagine with batch files.

What scripting languages are available?
Windows Script Host comes with built-in support for VBScript and Jscript, which, as you may know, are derivatives of the Visual Basic and Java programming languages. It’s important to keep in mind that because VBScript and JScript are scripting languages, the programs that you can develop with them are much simpler than those that you'd develop with the full-blown programming languages. However, these scripting languages are more than adequate for many tasks.

It’s also important to remember that while the VBScript and JScript scripting engines are built into Windows Script Host, you can add other scripting languages by using third-party add-ons. For example, you could add scripting engines for the Perl or Python scripting languages to Windows Script Host.

Creating scripts
Creating scripts for Windows Script Host’s built-in scripting engines is a snap. Since the scripts that you create in either VBScript and JScript files are just plain-text files with a VBS or JS extension, respectively, you can create and edit your script with any text editor, such as Notepad.

While it’s certainly beneficial to have some kind of programming background in order to create scripts, it’s not necessary. Because of the simple nature of scripts—and the availability of example scripts—almost anyone with substantial computing experience can develop scripts for automating tasks in Windows.

Downloading the Windows Script Host package
While Windows Script Host is built into Windows 98 and Windows 2000, you’ll definitely want to upgrade to the most recent version to take advantage of all the latest improvements. You can download the Windows Script Host package from Microsoft’s Windows Script Technologies site. Just follow the links to the Windows Script Host Downloads section. The Windows Script 5.5 package includes Windows Script Host 2.0, VBScript 5.5, and JScript 5.5. Keep in mind that there are two versions of the package, one for Windows 2000 and one for Windows 95/98/NT.

Resources
Here’s a look at some of the abundant information available on Windows Script Host.

Microsoft Windows Script technologies
In addition to the Windows Script 5.5 package, you’ll discover all sorts of information on Windows Script Host and its scripting languages on the Microsoft Windows Script Technologies site. You’ll also find sample scripts that will help you to understand the intricacies of the languages.

In addition, you can download the documentation files for the VBScript and JScript scripting languages. This documentation contains a users’ guide, a language reference, and other helpful information.

Newsgroups
Microsoft’s News Server contains a collection of newsgroups dedicated to the various Windows Scripting Technologies. Just point your newsreader to one of the following newsgroups:
  • Windows Script Host (microsoft.public.scripting.wsh)
  • VBScript (microsoft.public.scripting.vbscript)
  • JScript (microsoft.public.scripting.jscript)
  • Hosting Issues (microsoft.public.scripting.hosting)
  • Script Debugger (microsoft.public.scripting.debugger)
  • Windows Script Components (microsoft.public.scripting.scriptlets)
  • Remote Scripting (microsoft.public.scripting.remote)

Third-party Web sites
With the new awareness of the Windows Script Host as a viable replacement to batch files, a number of enthusiasts have created Web sites dedicated to providing information on the topic:

Books
Many authors have written quite a few books on the topic. Here are some you might want to investigate:
  • Teach Yourself Windows Script Host in 21 Days, by Thomas Fredell
  • Microsoft Windows Script Host 2.0 Developer’s Guide, by Gunter Born
  • Windows 2000 Scripting Bible, by William Robert Stanek
  • Windows Scripting Secrets, by Tobia Weltner
  • Windows 2000–Windows Script Host, by Tim Hil

Conclusion
If you’re ready to go beyond batch files as a means of automating certain tasks in Windows, you need to investigate Windows Script Host and its built-in scripting languages, VBScript and Jscript, as an alternative. In this Daily Drill Down, I’ve introduced you to Windows Script Host and explained how it works.

Future Daily Drill Downs will cover detailed step-by-step instructions on creating handy little scripts that you can use to automate your daily tasks and make your job easier.
The authors and editors have taken care in preparation of the content contained herein but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.

About Greg Shultz

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

Editor's Picks

Free Newsletters, In your Inbox