Windows Scripting Host Tutorial - Better Than Batch

Batch Jobs and the Evolution to Windows Scripting Host
For those with experience in the computing field, batch jobs are likely a familiar concept. IT professionals globally have long employed them to automate a wide range of computer processes and individual tasks.
However, traditional batch jobs possessed inherent limitations. Their command set was relatively concise, restricting the implementation of complex, structured logic.
Specifically, features like conditional statements (if-then), looping constructs (for, next, while) were not readily available or were difficult to implement effectively.
The Introduction of Windows Scripting Host
The Windows Scripting Host (WSH) emerged as a more versatile solution. Microsoft began including this multi-language scripting tool as a standard component with Windows 98.
Initially known as the MS Windows Scripting Host, the tool was later rebranded as Microsoft Script Host (MSH) with its second generation.
WSH provided a significant advancement over batch jobs, offering a richer scripting environment and greater flexibility for automation tasks.
This allowed for the creation of more sophisticated and powerful scripts capable of handling complex workflows.
Paul recently detailed the process of creating batch files, highlighting the foundational concepts that paved the way for more advanced scripting technologies like WSH.
Understanding the Microsoft Scripting Host
At MUO, we greatly value the power of automating computer processes. As an illustration, Varun previously detailed Sikuli, a tool designed for crafting automation scripts, and Guy demonstrated how to leverage AutoIt for task automation. A significant advantage of the Microsoft Scripting Host (MSH) is its accessibility; any PC running a version of Windows post-98 can execute "batch" scripts written in diverse languages.
The supported languages encompass JScript, VBA, and VBscript. Furthermore, it’s feasible to develop scripts utilizing Perl, Python, PHP, Ruby, or even Basic, provided you have the appropriate implementation and scripting engine installed.
My personal preference leans towards Visual Basic due to my familiarity with it, leading me to typically choose VBScript. A key benefit is the absence of a need for specialized programming software or a compiler. Simply open Notepad and compose your script, mirroring the process of creating traditional batch files.
Without requiring any additional installations, you can create scripts in VB. A fundamental script involves displaying text in a pop-up window, as shown below:

Saving the file with a .vbs extension allows Windows to recognize and execute it. This is the outcome when you double-click the aforementioned file:

You are capable of constructing more complex scripts utilizing the languages with which you are comfortable. For maximum versatility, enclose each code segment within <job> and <script language="VBScript"> (or your chosen language) tags, and save the file as a .wsf file. This approach enables the use of multiple languages within a single file, provided the code is properly tagged with its respective language.
To illustrate the potential, I developed a script that connects to the NIST atomic clock to retrieve the current time. If it's morning, the script automatically launches my Thunderbird email client. Conversely, at noon, it opens my web browser to CNN.com. This type of conditional script empowers you to imbue your computer with greater intelligence. Scheduling this script to run upon PC startup allows for automated launching of applications based on the time of day.

The initial portion of the script retrieves the current time from the time server at "http://time.nist.gov:13" and then formats and sets the computer's time. It’s important to acknowledge that this script was adapted from an excellent script by TomRiddle, available at VisualBasicScript.com. To expedite the process, always seek out existing code examples online and then modify them to suit your specific requirements.
Here's a demonstration of the script's functionality with the implemented code:

With the script now functioning and capable of synchronizing my PC each time it runs, the next step is to configure it to launch specific applications based on the time of day. Within the Windows Scripting Host, this is achieved through a simple If-Then statement that checks the hour using the "Now" function and then initiates the appropriate software.

Upon execution between 8:00 AM and 10:00 AM, this script will launch my Thunderbird email client. Running it between 11:00 AM and 1:00 PM will open CNN.com in a web browser. As you can observe, adding a degree of intelligence to a script file allows for the automation of various computer tasks.
It’s beneficial to have a reference of scripting commands readily available while developing these scripts. For those working with VBScript, ss64.com is a valuable resource, providing an alphabetical listing of all VBScript commands on a single page.

Simply writing scripts isn't sufficient for automation; you must also launch them. To complete the automation process using the Windows Script Host, access the Task Scheduler within the control panel (administrator area) and select the option to create a new task.

The scheduler allows you to initiate your script based on a variety of triggers, including the time of day, a specific schedule, system events, or upon computer startup or login. In this instance, I'm creating a scheduled task to launch my script each time the PC starts.

This tutorial provides only a brief introduction to the Windows Scripting Host. Given the extensive range of commands and functions available in these scripting languages, the possibilities for automating tasks on your PC are limited only by your creativity.
Here are some useful websites where you can find pre-written scripts for use or customization:
- Microsoft Script Center - Directly from Microsoft, offering categorized scripts for Office, desktop, databases, and Active Directory.
- Computer Performance - This UK-based site provides, in my opinion, the most comprehensive selection of VBScripts available online.
- Computer Education - A smaller collection of scripts, but highly useful and fully functional.
- Lab Mice - A fantastic resource for batch programming, including a variety of logon scripts.
Have you previously utilized the Windows Script Host? Do you have any valuable tips or examples to share? Please contribute your insights and experiences in the comments section below.
Image credit: jaylopez





