Introduction to Windows Script Host
WIndows Script Host is a core component of the Windows 2000 and is also available for earlier versions of Windows.
To ensure that WSH is installed on your computer, type cscript at a command prompt. You should see
version information for WSH as well as usage details
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Usage: CScript scriptname.extension [option...] [arguments...]
Options:
//B Batch mode: Suppresses script errors and prompts from displaying
//D Enable Active Debugging
//E:engine Use engine for executing script
//H:CScript Changes the default script host to CScript.exe
//H:WScript Changes the default script host to WScript.exe (default)
//I Interactive mode (default, opposite of //B)
//Job:xxxx Execute a WSF job
//Logo Display logo (default)
//Nologo Prevent logo display: No banner will be shown at execution time
//S Save current command line options for this user
//T:nn Time out in seconds: Maximum time a script is permitted to run
//X Execute script in debugger
//U Use Unicode for redirected I/O from the console
The key components of Windows Script Host are:
- WScript - A Windows executable for the scripting host that is used when you execute scripts from the
desktop. This executable has GUI controls for displaying output in pop-up dialog boxes.
- CScript - A command-line executable for the scripting host that is used when you execute scripts from
the command line. This executable displays standard output at the console.
- WSH Active Control - An ActiveX control that provides the core object model for the scripting host.
- Scripting Engines - Scripting engines provide the core functions, objects, and methods for a particulas
scripting language. VBScript and JScript scripting engines are installed by default on Windows 2000 and XP.
Windows scripts can be run with either WScript CScript, and most of the time the application
you use depends on your personal preference. However, you'll find that WScript works best for scripts that
interact with users. For tasks that you want to automate or run behind the scenes, you'll probably prefer
CScript, with which you can suppress output and prompts for batch processes.
The Basic Windows Script Host tasks are:
- Accessing Networks
- Creating an Automated Login Script
- Driving Applications
- Executing File Management Operations
- Managing Shortcuts
- Manipulating the System Registry
- Running Scripts Remotely
Windows Script Host Architecture
Core Model
WSH provides the key functionality to interact with the operating system. In WSH, objects are simply named containers
that you'll use to work with operating system components. For example, you'll use WSHNetwork object
to access and configure network resources, like printers and drives.
Each object has properties and methods that are used to perform certain types of tasks. Properties are
attributes of an object that you can access. Methods are procedures that you can use to perform operations. The following
table contains a summary of the WSH object model. The WSH hierarchy can be broken down into two broad categories:
- exposed objects, like WScript, are the ones you'll work with in your script.
- non-exposed objects, like WshCollection, are accessed through the methods or properties of
other objects.
| Exposed Objects |
| Object | Description |
| WScript |
Top-level object that provides access to core objects and other functionality such as object creation. |
| WScript.WshNetwork |
Automation object used to access and configure network resources, like printers and drives. Also provides user,
domain, and computer information. |
| WScript.WshShell |
Automation object that provides access to the environment and file folders. |
| Non-exposed Objects |
| Object | Description |
| WshArguments |
Accessed through WScript.Arguments. Obtains command-line arguments. |
| WshCollection |
Accessed through WshNetwork.EnumNetworkDrives or WshNetwork.EnumPrinterCollection.
Used for iteration through a group of items, such as printers or drives. |
| WshEnvironment |
Accessed through WshShell.Environment. Allows to work with environment variables. |
| WshShortcut |
Accessed through WshShell.CreateShortcut. Used to create and manage file shortcuts. |
| WshSpecialFolders |
Accessed through WshShell.Folder. Used to work with file folders. |
| WshUrlShortcut |
Accessed through WshShell.CreateShortcut | method. Used to create and manage URL
shortcuts.
More on scripting hosts
To execute Windows scripts, you'll use one of the two scripting hosts available, either WScript or
CScript. You can work with both of these hosts in much the same way, just enter wscript
or cscript followed by the pathname of the script you want to execute:
c:\scripts\> cscript my_first.js
Several file extensions are mapped for use with the scripting hosts. These file extensions are:
- .js - Designates scripts written in JScript.
- .vbs - Designates scripts written in VBScript.
- .wsf - Designates a Windows script file.
- .wsh - Designates a WSH properties file.
A limitation of .js and .vbs is that they can only contain JScript or VBScript statements,
respectively, and you cannot mix and match. That's why the .wsf files are useful. You can use .wsf
files to create WSH jobs, these jobs can combine multiple types of scripts and can also include type libraries
containing constants. Windows scripts can also use .wsh files. These files contain default settings for
scripts, such as timeout values and script paths. Because of the introduction of .wsf files and direct
in-script support for most script properties, .wsh files are rarely needed.
These are several small examples of Windows scripts:
- np.js - activates notepad and prints some text in it.
- net.wsf - prints some information about user, computer, and domain.
- sol.js - doesn't let you to play Solitaire.
- xl.wsf - opens up an Excel and works with it.
- WSH examples cite.
References:
- Windows® 2000 Scripting Bible by William R. Stanek. ISBN:
0-7645-4677-5.
A good book describing both JScript and VBScript in parallel. This book explains a lot of details in network
scripting and administration as well.
- Windows® Scripting Secrets by Tobias Weltner. ISBN:
0-7645-4684-8.
- Microsoft SDN library. Windows Script.