Better Than Batch: A Windows Scripting Host Tutorial
If you’ve been working in the computer world for a while then you’re probably pretty familiar with batch jobs. IT professionals around the world utilized them to run all sorts of automated computer processing jobs and personal tasks. In fact Paul recently covered how to write such a file.
The problem with batch jobs is that they were very limited. The command set was somewhat short and didn’t allow for very much functionality when it came to structured logic using if-then, for, next and while loops.
Later, Windows Scripting Host came along. The MS Windows Scripting Host is a multi-language script utility that Microsoft started installing as standard on all PCs from Windows 98 onward. By the second generation of the tool, it was renamed to Microsoft Script Host (MSH).
A Microsoft Scripting Host Tutorial
Here at MUO, we love computer automation. For example, Varun covered Sikuli, a tool to write automation scripts, and Guy showed you how to use AutoIt to automate tasks. The cool thing about MSH is that if you have any post-Win 98 PC, you can write a “batch” script in a variety of languages.
Available languages include JScript, VBA, and VBscript. It’s also possible to write scripts in Perl, Python, PHP, Ruby or even Basic if you have the right implementation with the right scripting engine.
Personally, I know Visual Basic well, so I usually opt for VBScript. The beauty here is that you don’t need any special programming software or compiler. Just open up Notepad and write your script, just like how you wrote your batch jobs.
Without installing anything, you can write scripts in VB. The simplest script is printing text to a pop-up window, like this:
Save the file as a .vbs and Windows will recognize and run it. This is what happens when you double click on the file above:
You can write more advanced scripts utilizing the languages you’re accustomed to. For the most flexibility, place <job> and <script language=”VBScript”> (or whatever language you choose) around each segment of code in your file, and save it as a .wsf file. This way, so long as you enclose the code in the defined script language tags, you can use multiple languages in the same file.
To show you how cool this can be, I decided to write a script that would reach out to the NIST atomic clock to check the current time. If morning, it automatically opens my Thunderbird email client. If noon, it would open my browser to CNN.com. This conditional script gives you the ability to make your computer much more intelligent. If you run this script when your PC starts up, you can make it automatically launch whatever you like depending what time of day it is.
The first part of the script goes out to the time server “” and gets the current time. After formatting it correctly, it sets the computer time. Credit where credit is due, this script was adapted from TomRiddle’s excellent script over at VisualBasicScript.com. To save time, always find the example code you need online, and then tweak it to your needs.
Here’s what the script does with just the code above implemented so far.
Now that the script is working and will sync my PC every time it’s launched, it’s time to have it determine what to automatically launch depending on the time of day. In Windows Scripting Host, this task is as easy as an If-Then statement checking the hour of the day in the “Now” function, and then launching the appropriate software.
When launched between 8 to 10 in the morning, this script will start up my Thunderbird email client. When run between 11am to 1pm, it’ll launch CNN.com in a browser. As you can see, just by being creating and adding a little bit of intelligence to a script file, you can do some pretty cool computer automation.
By the way, it’s a very good idea to have a reference of scripting commands handy when you write these scripts. If you’re into VBScript like me, a great resources is ss64.com, which lists all VBScript commands alphabetically on one page.
Writing scripts alone isn’t going to automate anything, because you’ll still have to manually launch them. So to complete your automation using the Windows Script Host, go into the Task Scheduler in the control panel (administrator area) and select to create a task.
The scheduler lets you launch your script upon a whole assortment of events, such as time of day or on a specific schedule, when a system event takes place, or when the computer is first booted or logged into. Here, I’m creating a scheduled task to launch my script above every time the PC starts.
This is only a very brief Windows Scripting Host tutorial. Considering the number of commands and functions available in any of these scripting languages, the possibilities to automate all sorts of cool tasks on your PC are pretty much only limited by your imagination.
Some of the best sites to find pre-written scripts that you can use or customize include the following:
- Microsoft Script Center – Straight from microsoft, and includes categories like Office, desktop, databases and active directory
- Computer Performance – This UK site offers the best selection of VBScripts that I’ve seen online.
- Computer Education – You’ll find a small collection of scripts here, but they’re very useful and they all work.
- Lab Mice – An awesome collection of batch programming resources like an assortment of logon scripts.
Have you ever used the Windows Script Host? Do you have any cool tips or examples to share? Offer your insight and share your experiences in the comments section below.
Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.