Download Windows Script Host samples

Microsoft Windows graphic

To download Windows Script Host samples

  1. In the navigation pane of the Microsoft Windows Script Technologies Web site (http://www.microsoft.com/), click Windows Script Host.
  2. On the Overview menu, click Samples.
  3. On the Samples page, click Download. The File Download dialog box appears.
  4. In the File Download dialog box, click OK. The Save As dialog box appears.
  5. In the Save As dialog box, either create a new folder or navigate to the folder where you want to save the samples, and then click Save. A file called Samples.exe is downloaded to your folder.
  6. Navigate to your Samples folder, and double-click the file Samples.exe. The WinZip Self-Extractor dialog box appears.
  7. In Unzip to Folder, enter the path name of your Samples folder. Click Unzip to extract the contents of Samples.exe.

Notes

  • Your Samples folder contains several JScript and VBScript files (you can delete Samples.exe). Your folder also includes Readme.doc, which contains information about the sample scripts.
  • You can examine and edit a script's code by right-clicking the script file in Windows Explorer and then clicking Edit.
  • You can run a script by double-clicking it in Windows Explorer.

Related Topics


Windows Script Host object model

The Windows Script Host object model gives you a logical, systematic way to perform many administrative tasks. The set of COM interfaces that it provides has two main categories.

Script execution and troubleshooting

Properties and methods directly related to script execution. This set of interfaces allows scripts to perform basic manipulation of the Windows Script Host, display messages on the screen, and perform basic COM 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.

Object What you can do with this object
WScript
  • Set and retrieve command-line arguments
  • Determine the name of the script file
  • Determine the host file name (Wscript.exe or Cscript.exe)
  • Determine the host version information
  • Create, connect to, and disconnect from COM objects
  • Sink events
  • Stop the execution of a script programmatically
  • Send information to the default output device (either a dialog box or the console)
WshArguments
  • Access the entire set of command-line arguments
WshNamed
  • Access the set of named command-line arguments
WshUnnamed
  • Access the set of unnamed command-line arguments
WshNetwork
  • Connect to and disconnect from network shares and network printers
  • Map to and disconnect from network shares
  • Access information about the user who is currently logged on
WshController
  • Create a remote script process using the Controller method CreateScript()
WshRemote
  • Remotely administer computer systems on a computer network
  • Programmatically manipulate other programs and scripts
WshRemote Error
  • Access the error information that is available when a remote script (a WshRemote object) terminates as a result of a script error
WshShell
  • Run a program locally
  • Manipulate the contents of the registry
  • Create a shortcut
  • Access a system folder
  • Manipulate environment variables, such as WINDIR, PATH, or PROMPT
WshShortcut
  • Programmatically create a shortcut
WshSpecialfolders
  • Access any of the Windows special folders
WshURLShortcut
  • Programmatically create a shortcut to an Internet resource
WshEnvironment
  • Access any of the environment variables, such as WINDIR, PATH, or PROMPT
WshScriptExec
  • Determine status and error information about a script that is run with Exec()
  • Access the stdIn, stdOut, and stdErr channels

In addition to the object interfaces provided by Windows Script Host, administrators can use any ActiveX control, that exposes 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

You can use Windows Script Host to run scripts by clicking a script file on the Windows desktop or by typing the name of a script file at the command prompt. Similar to Microsoft Internet Explorer, Windows Script Host serves as a controller of Windows Script-compliant scripting engines. Unlike Internet Explorer, Windows Script Host has very low memory requirements and is ideal for both interactive and non-interactive scripting, such as logon scripting and administrative scripting.

There are two versions of the Windows Script Host: a Windows-based version (Wscript.exe), which provides a property sheet for setting script properties, and a command-prompt-based version (Cscript.exe), which 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, its features are limited compared to VBScript and JScript. For example, the ability to control program flow was not built into the language. Today, the Windows Script Host architecture allows you to take advantage of these powerful scripting languages, although support is still provided for MS-DOS command scripts.

Windows Script Host supports scripts that are 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 contents of the specified script file to the registered script engine. The script engine uses file name extensions (.vbs for VBScript; .js for JScript) to identify the script instead of using the SCRIPT tag (used in HTML). Because of this, you do not have to be familiar with the exact programmatic identifier (ProgID) of the script engines. The script host itself maintains a mapping of script extensions to ProgIDs, and uses the Windows association model to start the appropriate engine for a given script.

This version of WSH adds several areas of new functionality:

Improved argument handling

Handling and documenting command-line arguments is now simpler.

Remote scripting

You can load a script onto several remote computer systems which you can then run at the same time.

A running process is an object

You can query the spawned process, sink its events, access its standard I/O streams, and share its environment variables with other processes.

The current working directory

You can determine and modify the path of the current working directory of the active process.

Script security issues

The new script security model makes it safer to distribute and run scripts.


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

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

You can run scripts with the command-line-based script host by typing the following at the command prompt:

cscript [script name] [host options] [script arguments]

Where:

  • script name is the name of the script file, including the file name extension and any necessary path information.
  • host options are the command-line switches that enable or disable various Windows Script Host features. Host options are always preceded by two slashes (//).
  • script arguments are the command-line switches that are passed to the script. Script arguments are always preceded by one slash (/).

Notes

  • Each parameter is optional; however, you cannot specify script arguments without specifying a script. If you do not specify a script or any script arguments, Cscript.exe displays the Cscript.exe syntax and the valid host options.
  • The command-line-based script host supports the following host options:
    Parameter | Action
    //B | Specifies batch mode, which does not display alerts, scripting errors, or input prompts.
    //D | Turns on the debugger.
    //E:engine | Specifies the scripting language that is used to run the script.
    //H:cscript or //H:wscript | Registers either Cscript.exe or Wscript.exe as the default script host for running scripts. If neither is specified, the default is Wscript.exe.
    //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 job identified by xxxx in a .wsf script file.
    //Logo | Specifies that the Windows Script Host banner is displayed in the console window before the script runs. This is the default and the opposite of //Nologo.
    //Nologo | Specifies that the Windows Script Host banner is not displayed before the script runs.
    //S | Saves the current command-prompt options for the current 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 | Starts the script in the debugger.
    //? Displays available command parameters and provides help for using them (this is the same as typing Cscript.exe with no parameters and no script).
  • The time out option (//T:nnnnn) prevents excessive execution of scripts by setting a time limit. When execution time exceeds the specified value, Cscript.exe interrupts the script engine and stops the process.
  • You can also use Windows Script Host to create .wsf script files, with which you can call multiple scripting engines and perform multiple jobs, all from one file.


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

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

You can run scripts with the Windows-based script host in the following ways:

By browsing in Windows

Browse to the folder containing the script you want to run and double-click it. Script files can be listed in My Computer, Windows Explorer, the Find window, the Start menu, or on your desktop.

By using the Run command

Click Start, and then click Run. In Open, type the name of the Windows Host executable file (Wscript.exe), followed by a space, then the full path name of the script you want to run. Be sure to include the file name extension of the script file. Click OK to start the script.

From a command prompt window

At the command prompt, type the name of the Windows Host executable file (Wscript.exe), followed by a space, then the full path name of the script you want to run. Be sure to include the file name extension of the script file. Press Enter to start the script.

Notes

  • Windows scripts are files with the following file name extensions: .wsf, .vbs, .js.
  • If you double-click a script file whose extension has not been associated with Wscript.exe, the Open With dialog box appears. Select Wscript.exe, then select Always use this program to open this file type. This registers Wscript.exe as the default script host for files of this file type.
  • You can use the Windows Script Host Settings dialog box to set global scripting properties for all scripts that Wscript.exe runs on the local computer.
  • You can set properties for individual scripts. See Related Topics for information about how to do this.
  • You can also use Windows Script Host to create .wsf script files, with which you can call multiple scripting engines and perform multiple jobs, all from one file.


Run Windows Script Host samples

To run Windows Script Host samples

  1. Command Prompt
  2. At the command prompt, type the following:

    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, including the file name 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

  • XOX
  • 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 global properties for all scripts that are run with the Windows-based script host (Wscript.exe)

To set global properties for all scripts that are run with the Windows-based script host

  1. Click Start, and then click Run
    2. .
  2. In the Open box, enter wscript, and then click OK. The Windows Script Host Settings dialog box appears.
  3. Select the Stop script after specified number of seconds check box.
  4. In the seconds box, enter the time limit you want to place on all scripts.

Notes

  • With one setting, you can set properties for all scripts that are run with the Windows-based script host. Every script that is run with this host automatically uses these settings.


Set properties for individual scripts that are run with the Windows-based script host (Wscript.exe)

To set properties for individual scripts that are run with the Windows-based script host

  1. Windows Explorer. Browse to the folder with the script whose properties you want to set, and right-click it.
  2. Select Properties, and then click the Script tab.
  3. Select the Stop script after specified number of seconds check box.
  4. In the seconds box, enter the time limit you want to place on this script.

Notes

  • XOX
  • These settings override global settings.
  • After clicking OK, a command file is created that binds the property settings to the script. It has the file name extension .wsh. You can use this command file as an alternate script file to run the script.