Download Windows Script Host samples

To download Windows Script Host samples

  1. Go to the Windows Script Technologies Web site at the Microsoft Web site (http://www.microsoft.com/), click the Windows Script Host link, click the Samples link, click the Download the samples link, and then in the File Download dialog box, click OK.
  2. In the Save As dialog box, point to the folder where you want the sample files to be saved, and then click Save.
  3. Open My Computer, open the folder where you saved samples.exe, and then double-click the file to expand the sample scripts.
  4. In Unzip To Folder, type a path where you want the sample scripts to be saved, and then click Unzip.

Related Topics


Windows Script Host object model

The Windows Script Host object model provides two main categories of ActiveX interfaces:

Script execution and troubleshooting

Properties and methods that are directly related to script execution. This set of interfaces allows scripts to perform basic manipulation of the Windows Script Host, print out messages to the screen, and perform basic functions such as CreateObject and GetObject.

Helper functions

Properties and methods for performing actions such as mapping network drives, connecting to printers, retrieving and modifying environment variables, and manipulating registry keys. Administrators can also use the Windows Script Host helper functions to create simple logon scripts.

In addition to the object interfaces provided by Windows Script Host, administrators can use any ActiveX controls that expose Automation interfaces to perform various tasks on the Windows platform. For example, administrators can write scripts to manage the Active Directory directory services.

For more information, see the Windows Script Technologies Web site at the Microsoft Web site (http://www.microsoft.com/).


Windows Script Host overview

Windows Script Host enables scripts to be run directly in Windows 2000 by clicking a script file on the Windows desktop, or by typing the name of a script file at the command prompt. Just like Microsoft Internet Explorer, Windows Script Host serves as a controller of ActiveX scripting engines. Unlike Internet Explorer, however, Windows Script Host has very low memory requirements and is ideal for both interactive and non-interactive scripting needs such as logon scripting and administrative scripting.

Windows Script Host supports scripts written in Visual Basic Scripting Edition (VBScript) or JScript. When a script is started from the Windows desktop or from the command prompt, the script host reads and passes the specified script file contents to the registered script engine. The scripting engine uses file extensions (.vbs for VBScript; .js for JScript) to identify the script instead of using the SCRIPT tag (used in HTML). This way, the script writer doesn't have to be familiar with the exact programmatic ID (ProgID) of various script engines. The script host itself maintains a mapping of script extensions to ProgIDs and uses the Windows association model to launch the appropriate engine for a given script.

There are two versions of the Windows Script Host: a windows-based version (wscript.exe) that provides a windows-based property sheet for setting script properties, and a command prompt-based version (cscript.exe) that provides command line switches for setting script properties. You can run either of these by typing wscript.exe or cscript.exe at the command prompt.

Previously, the only native scripting language supported by the Windows operating system was the MS-DOS command language. Although MS-DOS is fast and small, it has limited features compared to VBScript and JScript. Today, ActiveX scripting architecture allows users to take advantage of these powerful scripting languages while support is still provided for MS-DOS command scripts.

For more information, see the Windows Script Technologies Web site at the Microsoft Web site (http://www.microsoft.com/).


Run scripts using the command-line-based script host (cscript.exe)

To run scripts using the command-line-based script host (Cscript.exe)

  • Type the following at the command prompt:

    cscript [script name] [host parameters] [script parameters]

    Where:

    script name is the name of the script file, complete with extension and any necessary path information.

    host parameters are the command line switches that enable or disable various Windows Script Host features. Host parameters are always preceded by two slashes (//).

    script parameters are the command line switches that are passed to the script. Script parameters are always proceeded by one slash (/).

Notes

  • Each parameter is optional; however, you cannot specify script parameters without specifying a script. If you do not specify a script or any script parameters, Cscript.exe displays the Cscript.exe syntax and the valid host parameters.
  • The command-based Cscript.exe supports the following host parameters:
    Parameter | Action
    //B | Specifies batch mode, which will not display alerts, scripting errors, and input prompts.
    //D | Turns on the debugger.
    //E:engine | Specifies the scripting language used to run the script.
    //H:cscript or //H:wscript | Registers Cscript.exe or Wscript.exe as the default application for running scripts. If neither is specified, Wscript.exe is assumed as the default.
    //I | Specifies interactive mode, which displays alerts, scripting errors, and input prompts. This is the default, and the opposite of //B.
    //Job:xxxx | Runs the specified job.
    //Logo | Specifies that the banner will display at run time. This is the default, and the opposite of //Nologo.
    //Nologo | Specifies that no banner will display at run time.
    //S | Saves the current command prompt options for this user.
    //T:nnnnn | Specifies the maximum time the script can run in seconds. You can specify up to 32,767 seconds. The default is no time limit.
    //X | Launches the script in the debugger.
    //? Displays available command parameters and provides help for using them (same as typing Cscript.exe with no parameters and no script).
  • The time out (//T:nnnnn) option prevents excessive execution of scripts by setting a timer. When execution time exceeds the specified value, Cscript.exe interrupts the script engine using the IActiveScript::InterruptThread method and stops the process.
  • Windows Script Host also enables you to create .ws script files, with which you can call multiple scripting engines and perform multiple jobs, all from one file. For more information about these files, see the Windows Script Technologies Web site at the Microsoft Web site (http://www.microsoft.com/).


Run scripts using the Windows-based script host (Wscript.exe)

To run scripts using the Windows-based script host (Wscript.exe)

  1. From the command prompt or the Run dialog box, type:

    wscript.exe

    and then click OK or press ENTER on your keyboard.

  2. Set the script host properties you want, and then click OK.
  3. From Windows Explorer or My Computer, double-click the script file you want to run.

Notes

  • If you double-click a script file whose extension has not yet been associated with wscript.exe, an Open With dialog box appears asking you what program you would like to use to open the file you double-clicked. After choosing Windows Based Script Host (wscript.exe), if you check the Always use this program to open these files check box, wscript will be registered as the default application for all files having the same extension as the one you double-clicked. For example, if you check this check box when you run chart.vbs, wscript.exe becomes the default application for all files having the .vbs extension.
  • The wscript.exe properties page lets you set global scripting properties for all scripts that wscript.exe runs on the local computer. It is also possible to set properties for individual scripts. See Related Topics for information on how to do this.
  • You can also set properties for an individual script by right-clicking a script file in My Computer or Windows Explorer, clicking Properties and then selecting the Script tab.
  • Windows Script Host also enables you to create .ws script files, with which you can call multiple scripting engines and perform multiple jobs, all from one file. For more information about these files, see the Windows Script Technologies Web site at the Microsoft Web site (http://www.microsoft.com/).


Run Windows Script Host samples

To run Windows Script Host samples

  1. Click Start, point to Programs, point to Accessories, and then click Command Prompt.
  2. Type the following at the command prompt:

    cscript samples folder\script name

    Where:

    samples folder is the path to the folder into which you downloaded the samples.

    script name is the name of the sample script you want to run, complete with the file extension (for example, .js or .vbs).

    For example, if you saved the samples to C:\Samples, and you wanted to run the Network.vbs sample, you would type:

    cscript c:\sample\network.vbs

Notes

  • You must download the Windows Script Host sample files from the Microsoft Web site before you can run them.
  • You can also double-click the sample script file to run a script using the wscript.exe script host.


Set properties for individual scripts

To set properties for individual scripts

  1. In Windows Explorer or My Computer, right-click the script file for which you want to specify individual properties, and then click Properties.
  2. Click the Script tab, set the options you want to use for that script, and then click OK.

Notes

  • The property settings are saved in a file with a .wsh file extension. For example, if the script file name is Chart.vbs, the settings are saved in a text file called Chart.wsh.
  • A .wsh file is a text file that uses a format similar to that of .ini files. A .wsh file contains a [ScriptFile] section, which identifies the script file that the .wsh file is associated with, and an [Options] section, which correspond to the settings you selected in the Script tab.
  • A .wsh file is analagous to the .pif files used to run older 16-bit applications. It can be treated as if it were an executable or batch file. For example, suppose you have a script named Myscript.vbs for which you have created a .wsh file named Myscript.wsh. To run Myscript.vbs with the options recorded in Myscript.wsh, you can either double-click Myscript.wsh in Windows Explorer, or pass Myscript.wsh as a script argument to Cscript.exe or Wscript.exe at the command prompt or the Run dialog box.