Running Scripts from Windows or a Command Prompt

Windows Script Host enables you to run scripts from Windows or at a command prompt.

Script Hosts

The WScript command opens separate windows for output, whereas the CScript command sends output to the Command Prompt window from which it was initiated.

On initial installation of Windows Script Host, the default host is WScript. See To Change the Default Host later in this topic for more information.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

Running Scripts

To run a script from Windows Explorer or My Computer

  • Double-click the script file name. The default host (WScript or CScript) is used for script output.

To run a script at a command prompt

  • At a command prompt, type WScript or CScript followed by the script file name, as shown in the following examples.

    WScript sample.js
    CScript c:\scripts\sample.js
    
  • You can also specify only the script file name, as shown in the following examples.

    sample.js
    c:\scripts\sample.js
    
  • If you are using Windows NT or Windows 2000, the script file name should not include the extension. It should resemble the following example.

    sample
    

To run a script from the Run dialog box

  1. Click Start, and then click Run.

  2. In the Run dialog box, type the script file name, or type WScript or CScript followed by the script file name.

To Change the Default Script Host

On initial installation of Windows Script Host, the default script host is WScript. To change it to CScript, type the following at a command line. (On computers running Windows Vista, open the command prompt by using Run as administrator.)

cscript //h:cscript

To change the default host from CScript to WScript, type the following at a command line.

cscript //h:wscript

Command-Line Syntax

Type a command at a command prompt by using the following syntax.

cscript [host options...] [script name] [script options and parameters]
wscript [host options...] [script name] [script options and parameters]

The script name is required.

Host Options

Host options enable or disable various Windows Script Host features. Host options are preceded by two slashes (//). To display the valid host options, type CScript without parameters at a command line.

The following table lists the host options parameters.

Parameter

Description

//B

Batch mode. Suppresses command-line display of user prompts and script errors. The default is Interactive mode.

//D

Enables Active Debugging.

//E:engine

Executes the script by using the specified script engine.

//H:CScript

Changes the default script host to CScript.

//H:WScript

Default. Changes the default script host to WScript.

//I

Default. Interactive mode. Enables display of user prompts and script errors. Opposite of Batch mode.

//Job:JobID

Runs the specified JobID from the .wsf file.

//Logo

Default. When used with CScript, outputs a command-line banner that includes the Windows Script Host version number. Opposite of NoLogo.

//NoLogo

Prevents display of a banner at run time. Default is Logo.

//S

Saves the current command-line options for this user.

//T:nn

Enables time-out: the maximum number of seconds the script can run. nn is the maximum number of seconds.

The default is no limit. The //T parameter prevents excessive execution of scripts. When execution time exceeds the specified value, CScript interrupts the script engine by using the IActiveScript::InterruptThread method and terminates the process.

//U

Forces the command-line output to be in Unicode.

//X

Launches the script in the debugger.

/?

Displays a brief description of and usage information for command parameters.

WScript Properties

To set WScript properties for an individual script, right-click the script file name. On the Properties menu, click the Scripting tab. To set WScript properties for all scripts that WScript runs on a given computer, type WScript at a command prompt.

The following table describes the options in the WScript properties dialog box:

Dialog box option

Description

Stop script after the specified number of seconds.

Specifies the maximum number of seconds that a script can run. The default is no limit.

The CScript equivalent is //T:nn.

Display logo when the script is executed in command console.

Displays a banner before the script is run. This is the default. The opposite is //NoLogo.

The CScript equivalents are //Logo or //NoLogo.

See Also

Concepts

Scripts and Automating Windows

Other Resources

Reference (Windows Script Host)

Change History

Date

History

Reason

April 2009

Consolidated content from three other topics and reorganized content.

Information enhancement.

March 2009

Fixed the command that changes the default host to WScript.

Content bug fix.