Better Than Batch: A Windows Scripting Host Tutorial

Ryan Dube 16-12-2010

windows scripting host tutorialIf 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 Create a Batch (BAT) File in Five Simple Steps This article covers how to create a batch file using five simple steps for the Windows operating system. Read More 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 How to Automate Mundane Tasks with AutoIt Read More 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:

windows scripting host tutorial

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:


windows scripting

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 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.

windows scripting


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 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.

windows scripting

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.


windows scripting host

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 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, which lists all VBScript commands alphabetically on one page.

windows scripting host

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.

windows scripting host

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.

windows scripting host tutorial

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.

Image credit: jaylopez

Related topics: Computer Automation, Microsoft, Programming.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

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

  1. SMK
    January 16, 2019 at 12:22 pm

    Took me 10 minutes to work this out as above not totally clear... I initially opened with and closed with which I did think was odd. Before I realised perhaps the obvious but not to a beginner with WSH.

    wscript.Echo "Hello World!"

  2. Wahid
    May 27, 2016 at 3:45 am

    Thanks, so helpful for a beginner like me

  3. Kathiravan
    May 25, 2016 at 2:08 pm

    hi...I am new to all scripting languages. I am assigned to a automation in my work area. which language will be helpful for me ? I am considering WSH, VBS and JS.. Which one should I go through ? I mean which one doesn't have much limitation ?

    • Ryan Dube
      May 26, 2016 at 12:18 am

      It really depends on the application you are looking to create. VBS is great for the automation of office products, but WSH is really effective for Windows OS stuff like checking system specs of remote systems on the network. It all depends on what you want to do, and that should determine which language/tools you choose.

  4. Jalel
    May 24, 2015 at 8:11 pm

    Thanks for your post it was helpful.

  5. Joey
    March 31, 2015 at 5:26 am

    thanks .. Im a beginer to VBS , this has been very useful
    - Joey_reddy

  6. Vinaybisht155
    February 16, 2011 at 12:31 pm

    Its vinay
    thanks for ur emails................

  7. Vinaybisht155
    February 16, 2011 at 1:31 pm

    Its vinay
    thanks for ur emails................