WSH in Other Languages

Much of the appeal of WSH lies in its simplicity. However, much of its power lies in its flexibility. Most often you will see WSH scripts written in VBScript, but in reality the Windows Script Host supports almost any COM-enabled scripting language.

Today we’re going to take a look at using WSH in other languages. WSH has built-in support for VBScript and JScript. These languages can be used out-of-box. In order to use other languages you must install a WSH-compatible engine for the language. This is also sometimes called an interpreter.

Many scripting languages have WSH-compatible engines available. When executing a script, the Windows Script Host determines which engine to use based on the script’s file extension. If the script uses a registered file extension, WSH is often able to determine this automatically.

Be aware that common file extensions may be associated with the original language runtime and not with the Windows Script Host. If not, you can specify what engine to use on the command line.

cscript /engine:PerlScript

Executing a script with a .pl file extension will execute it in the PerlScript runtime environment. The command line above will instead execute it within the WSH environment.

You might consider creating a file association for specific file extensions to be used with WSH. For example, you might use .pls for PerlScript files that you intend to run in WSH. This will avoid possible confusion and allow you to run your scripts without the additional command-line parameter.

This article is by no means comprehensive. I’m going to profile some of the more common languages that readers may want to use and provide a basic introduction that should get you up and running in the WSH environment. It would be impossible to teach every one of these languages within this article.  Instead, I’m going to assume that readers have some exposure to the language and demonstrate how to use the WSH-compatible components.

{mospagebreak title=VBScript}

Microsoft Visual Basic Scripting Edition (VBScript) is an Active Scripting language developed by Microsoft in 1996 with the release of Internet Explorer 3.0. The syntax is a subset of Microsoft’s Visual Basic programming language.

VBScript is the most commonly used language in WSH because it is easy to learn and well-integrated throughout the Microsoft family of software. Its real-world command set and non-strict nature allow novice users to begin writing code very quickly.

The VBScript engine comes integrated into the Windows Script Host environment.  Simply naming a plain text file with a .vbs extension is all that is required to make the script executable.

Accessing COM Objects

As an object-oriented environment, WSH relies heavily on its ability to use COM objects installed on the system.  While the most commonly used objects are OLE automation objects, WSH (and VBScript) will allow you to connect to and use any COM-enabled library that has a scripting interface.

Set objFso = CreateObject("Scripting.FileSystemObject")

In order to use objects and access their various properties, methods, and events you must first create an instance of the object in your script.  This is most commonly done using VBScript’s CreateObject method.

Because VBScript is fully integrated into the WSH environment, intrinsic objects such as WSH’s WScript object model are readily available and do not require initialization.

VBScript also provides a GetObject method that returns a reference to an object instance that is already running.

Set objFolder = objFso.GetFolder("C:temp")

The properties and methods of an object are accessed using standard object-dot syntax. In this case, the FileSystemObject’s GetFolder method is being used to return a Folder object that represents a supplied system path.

Set colFiles = objFolder.Files

Finally, the Folder object’s Files property is used to return a collection object containing object references for each of the files in the specified folder.

Working with Collections

Various scripting languages handle collection objects differently. Since collections are quite commonplace in the WSH environment, I’m including a section that covers this topic for each of the languages I present today.

For Each objFile In colFiles

   WScript.Echo objFile.Name


In VBScript, collections may be accessed directly. It provides a For Each…Next structure for iterating through each object in the collection.  Here, I’m quite simply just printing the file name back to the screen.

Additional Notes About VBScript

VBScripts provides a very relaxed programming environment. VBScript is not case-sensitive, nor does it require any line endings. It also ignores extraneous whitespaces; however, statements may not span more than one line without using the special underscore character. Finally, variables are all of type variant and may be explicitly declared, but this is not required.

{mospagebreak title=Jscript}

Jscript was also released in 1996 by Microsoft to meet the ECMAScript scripting programming language specifications.  It most closely resembles Netscape’s own ECMAScript solution known as JavaScript.  While the terms Jscript and JavaScript are often erroneously used interchangeably, it is important to remember that the two represent completely different and independent entities.  Despite their close resemblance, Microsoft retained enough differences to keep the two languages incompatible.

Because of Jscript’s wide use as a web development language, its support was also included in WSH natively.  This makes it the perfect alternative to VBScript for many administrators.

var fso = new ActiveXObject("Scripting.FileSystemObject");

An object can be accessed by creating a new instance of the object with the ActiveXObject method.  Just as in VBScript, an object is typically identified by its ProgID.

var folder = fso.GetFolder("C: emp");

Like VBScript, Jscript uses standard object dot syntax to access an object’s properties and methods.

Working with Collections

Unlike VBScript, Jscript cannot access collection objects directly.  You must first create an Enumerator object.

var e = new Enumerator(folder.files);

An enumerator object provides a mechanism for accessing and manipulating the objects found within a collection.

for (e.moveFirst(); ! e.atEnd(); e.moveNext()) {

   var file = e.item();



Iterating through the enumerator’s items is then done using a For structure.

Additional Notes About Jscript

All statements in Jscript require a semi-colon line ending and parenthesis should be used to denote all methods even when you are not supplying parameters.

Unlike VBScript, Jscript does maintain some case sensitivity.  Variables and object names are case sensitive, while an object’s property and method names are not.

It is also important to remember that the forward slash is a reserved character and must be escaped whenever it appears in a string.

{mospagebreak title=Python}

Python development began in the late 1980s; it was first published in 1991.  It is a high-level programming language that focuses on productivity rather than readability.  It supports multiple programming paradigms, automatic memory management, and has a large, comprehensive standard library. These features have led Python to become one of the most commonly used open-source environments.

While Python is widely used in the Linux community, it is becoming more and more popular to Windows programmers as well. The feature-rich language helps bridge a gap between the two communities while offering a feasible open source solution to Microsoft’s proprietary languages. With increased support in the Windows community, Python is now gaining a lot of momentum as a viable alternative for WSH programming.

The most commonly used Windows distribution is ActiveState’s ActivePython.  This free distribution offers the Python core, many popular extensions, and complete documentation.  It also includes PyWin32, a set of extensions that provides Python bindings to much of the Windows API, and the ActivePython installer adds support for use in the Windows Script Host environment.

WSH will not automatically detect a script written in Python, so you will have to specify the Python engine when launching the script, as I explained earlier in this article.

cscript //E:Python

The command line above can be used to launch a Python script in WSH.  You might also consider adding a unique file extension for Python scripts written in WSH.  I’ll show you how you can do that in part two of this series.

fso = WScript.CreateObject("Scripting.FileSystemObject")

You’ll have to use the WScript object’s CreateObject method to connect to COM objects. The WScript object is immediately available whenever a Python script is run in the WSH environment.

folder = fso.GetFolder("C: emp")

files = folder.Files

Python’s object dot syntax should look refreshingly familiar when accessing an object’s properties and methods.

Working with Collections

Like VBScript, Python is able to work with collections and its simple For structure makes it easy to iterate through each of the collection’s objects.

for file in files:


You may use either the WScript object’s Echo method or Python’s native print command for outputting text to the screen.

Additional Notes About Python

Python does not require line endings and is pretty flexible about whitespace.  Case sensitivity is much like Jscript in that it does require objects to be case sensitive while their properties and methods are not.

One caveat for Python users is that you cannot set an object’s property value directly. You must instead use the SetValue method as shown below.

object.SetValue("property name", newvalue)

Finally, a nice feature of Python is that it automatically imports all constants whenever you create an instance of an OLE object. These are created as properties of win32com.client.constants.


So any time that you create an instance of the Microsoft Word automation object, the above constant will be defined.

{mospagebreak title=More Languages}

WSH support for additional languages does not stop here by any means.  I’ve only begun to show you the most common languages used by administrators.  Virtually any COM-enabled language with support for Active Scripting can be used.  Check the documentation for your favorite language.  Most Windows installers will add support for your language if it is available.

Whether you’re looking to increase power and productivity, or simply wanting to use a language with which you are more comfortable, WSH provides a lot of flexibility for using additional languages.

This support is carried beyond simple code execution; once installed, these additional languages are available to WSH job files (WSF), Windows Script Components (WSC), and HTML Applications (HTA), and within Internet Explorer.  For Vista users, this also means that your additional languages are available for Vista Sidebar gadgets as well.

That’s all the space I have for now.  Be sure to stick around for the second part of this series when I show you how to use other common languages such as Object Rexx, KixStart, Ruby, and Perl.  I’ll also show you how creating a special file association can simplify code execution in WSH.  You won’t want to miss it.  Until next time, keep coding!

One thought on “WSH in Other Languages

  1. The Windows Script Host is not limited to VBScript and JScript. In fact, you can use nearly any scripting language you like: Perl, Ruby, Rexx, or even Python to name a few. Let the Windows Guru show you how!

[gp-comments width="770" linklove="off" ]