Scripts for task automation in Windows. Cool WSH features


Probably many people know, that Windows 98 and later includes Windows script host (WSH) by default, which allows to run VBScript and JScript code, but few ones used it at least once. In this article I am going to show you useful WSH snippets and script examples and convince you that this feature is really worthwhile. I will also tell you about very useful and cool WSH features, which are almost unknown, and therefore it is not easy to find information about them on the Internet.

First, a little about languages supported by WSH. JScript is actually JavaScript with slightly modified object model (for example, JScript does not include window object, like browsers, but it has WScript object, which allows to interact with script environment). VBScript is based on Visual Basic 6 (and possibly earlier versions) syntax and features. Both languages have similar capabilities. Besides that, you can install other languages for WSH, for example, PerlScript, which, as you have figured out already, is based on Perl. To perform this you should use, for example, ActiveState Perl installer:


JS file extension is associated with JScript scripts, VBS – with VBScript in Windows by default. If PerlScript is installed, PLS extension becomes associated with PerlScript. You can encode JS and VBS scripts using screnc.exe Microsoft utility and get a file with JSC or VBE extension respectively. Unfortunately, such encoding can protect only from inexperienced users – many decoders can be found over the Internet. Besides that, encoding text in languages other than English may have issues.

Another great WSH feature is to combine code in all languages installed on system in one WSF file. For example, VBScript has a function to display a text input window (InputBox), and you need it, but you write your script in JScript, which does not have such function. This problem is very easy to solve – you have to create WSF file with such content:

For example, you can combine JScript and PerlScript in the same way, if you have PerlScript installed:

Let’s take a brief disgression: I’m going to tell you about how to run WSH scripts. They are launched on double-click with wscript.exe. In this case all WScript.Echo calls are translated to common messagebox’es. If you use a script to automate some process, for example, a project build, and you want to display multiple messages, you should run this script with cscript.exe, which translates WScript.Echo calls to console outputs. You can run a script via console by creating BAT file with code like this:

You can also right-click a script file and use menu option “Open with command prompt”.

Probably this will surprise someone, but .NET classes can be used easily with WSH! I’ll show you JScript example (JS file):

We will see this form after executing this script:


And all this is created with .NET-classes! In fact, there are some complications. Firstly, I haven’t found any way to use delegates, which process .NET events. Secondly, there is no way to call COM object constructor, which can take parameters, via CreateObject (by the way, this is related not only to .NET). Thirdly, only a limited number of .NET classes and assemblies is available by default:


On the other side, it is really simple to publish .NET assembly to make it available via COM interfaces. If Windows Forms script presented above did’t work (which is most possibly true), type this command in console:

or, under 64-bit operating system:

This command publishes System.Windows.Forms assembly, which becomes available via COM interfaces, then you can use its classes in your scripts. Unfortunately, you can register only assemblies which have ComVisible=true attribute (if assembly has such attribute, this is stated on respective MSDN assembly description page).

To cancel assembly registration run mentioned above command with /unregister key.

What else do you have to know when using .NET assemblies? Frequently .NET classes have overloaded functions with same names and different parameter types and count. How can we call them? After all, script variables are almost not typified! This is very simple. For example, let’s take System.Random class. It is registered by default and available from WSH. It has three methods named Next. How can we call the right one? .NET maps identical methods names in such way: first one from the end of method table is named Next (in this example), the next one – Next_2, further is Next_3. How can we see this table with proper function order? For example, using ildasm:


Here I opened mscorlib.dll assembly from %WINDIR%\Microsoft.NET\Framework64\v2.0.50727, after that I navigated to System namespace and located Random class in it. Now it is clear – if we want to use Next method, which allows to set minimal and maximal values for random generation, we should use Next_2 (because this method is the second from the end in Next method list, see screenshot above):

Another interesting possibility of WSH scripts is drag&drop support. You can drag different files on JS, VBS, JSE, VBE, WSF files and their names will become available through WScript.Arguments.

So, let’s sum it up. What is remarkable in writing scripts in JScript, VBScript or PerlScript? Why it is better and easier than using BAT files or PowerShell?

[+] You can choose your familiar favorite syntax. You prefer JavaScript – use it! You adore Visual Basic – then VBScript is for you!
[+] You can combine code written in all these languages in one script, thus expanding one language possibilities with features of another one.
[+] All standard language options are available for you. External programs execution support, binary and text files operations, regular expressions and even more is included. Masked operations on files, operations with network paths (samba, for example), too.
[+] You can use lots of registered COM classes.
[+] You can use a number of COM-Visible .NET assemblies.
[+] You can work with WMI, because it is available through COM.
[+] There is full Unicode support, you just have to save a file as Unicode Little Endian.

Lots of evident benefits. WSH can be used to write powerful applications and scripts, which will make your life easier and perform some useful tasks automatically.

Now I just have to show you several useful snippets, which will be handy if you decide to use WSH for writing scripts to perform automated project building/parsing/file operations etc. In this example I will use my favorite JScript, because it has most common syntax and it will be clear to most of you. Besides that, JScript provides convenient way to catch exceptions, which can be thrown by COM classes and WScript object functions, using try-catch. Also, you can throw exceptions by yourself (this is standard JavaScript feature, and of course it is included in JScript).

1. Working with file system.

2. Working with environment variables.

3. External program execution

Run a program, wait until it finishes and get its return code:

Execute external program and capture its output:

You can also input any data to program via Exec.StdIn.

4. Working with Windows registry.

5. Working with WMI.

List all nested registry keys in specified branch:

Curiously, but this thing is much easier to do in VBScript:

Execute WMI query (in this example – print all accounts of a computer):

At this point I will complete my narration. I think, if you coped with whole article to its end, you realize all the power and convenience provided by WSH scripts and will certainly use this awesome Windows functionality!
For further study I can recommend MSDN (there are lots of WSH documentation, and, of course, .NET and WMI), and Google (there are many WSH script examples). Good luck in studying!

Leave a Reply

Your email address will not be published. Required fields are marked *