Pin Me

Windows Scripting Host (WSH)Tutorial

written by: •edited by: J. F. Amprimoz•updated: 8/19/2010

In this Windows Scripting Host tutorial, you will learn the main purpose of WSH, what its uses are, and how a WSH supported script is written. In addition to that, I will also show you a few basic VBScript and JScript examples, and describe how to run them

  • slide 1 of 3

    Introduction

    The very first time people learn about Windows Script Host, they often express confusion. What is WSH? Is it an object model like ADSI orwscript  WMI? No! WSH does provide a very basic object model, but providing an object model isn’t its main purpose. Is it a language like VBA or JavaScript? No! Although WSH allows you to run programs written in these languages, it’s not a language itself.

    So then what exactly is WSH, what is its primary purpose, usage and how is it used? Here is the explanation: Windows Script Host is an automation technology for Windows operating systems. It plays the part of the host for a script, makes services and objects available and provides a set of rules within which users can execute scripts.

    WSH provides scripting capabilities similar to batch files, but with greater range of features. You’re probably familiar with other script hosts. Shell programs (Bourne, Korn or C Shell), for example enable you to write and execute scripts, which use an object model that can manipulate the file system. Microsoft Internet Explorer enables you to run scripts, which use Dynamic HTML object model. Even the Windows command prompt can be called a script host because it provides environment, where scripts written in a batch file can be run.

    WSH is a unique script host because unlike other script hosts mentioned above, it is language independent and can make use of many Active Scripting language models, such as Perl, PHP, Ruby, Python, Delphi and other languages. By default, WSH can interpret VBA, Jscript and VBScript. Another advantage of WSH is that it allows you to use a combination of scripting languages in a single file. Plus, it gives you the flexibility of running scripts from both the command prompt (using cscript.exe) and Windows GUI (using wscript.exe).

  • slide 2 of 3

    Usage

    Windows Script Host is best for non-iterative scripting needs, such as machine automation, administrative scripting and logon scripting. For a basic Windows user, WSH may be used for a variety of purposes such as mapping network drives, managing files and folders, modifying registry keys, modifying printer settings, disable or enable a Windows feature, and managing environment variables, etc

  • slide 3 of 3

    Examples

    These examples are simple and straightforward. They use VBScript and JScript to display a message with an OK button. When you double-click on the script, the script host would be called, and the script will be executed.

    Here is the example VBScript:

    WScript.Echo “Welcome to Windows"

    WScript.Quit

    Here is the same program in JScript.

    WSH.Echo (“Welcome to Windows");

    WSH.Quit();

    Note: The VBScript program is saved in .vbs file and the JScript file is saved in a .js file.

    You can also mix JScript and VBScript (and any other) in a single .wsf file. However, the format of the code in a .wsf file slightly changes, as you will see in the code below that this code resembles XML:

    <userjob1>

    <script language="JScript">

    WSH.echo("How are you message from JScript");

    </script>

    <script language="VBScript">

    MsgBox "How are you message from VBScript"

    </script>

    </userjob1>

    You see how easy it is to write a script. To run your script, you can either go to the command prompt and enter “cscript yourscriptname.extension", or open the RUN window and enter wscript yourscriptname.extension. For example: wscript folderdelete.vbs

    If you are familiar with basic C or VB programming, then writing these scripts would be no problem for you. To learn more about WSH, JScript or VBScripts, you may check out Microsoft.com, Technet.com, W3Schools.com.

    I hope you found this windows scripting host tutorial easy. If you have any question about WSH or want to share anything about it, you may write your message in the comments section below.



Popular Pages



More Info