Toggling the Scripting Host
William Sheldon explains the difference between CScript and WScript, shows you how to toggle the default host environment from the command line, and shows you how to make the change permanent.
January 2, 2001
When it comes to scripts, most people think about VBScript versus JScript or some other difference in language. However, the scripting environment has two main components: a language interpreter and a host environment (what I roughly describe as the interface to the OS). Windows Script Host (WSH) supports two environments: WScript and CScript. WScript is the system default on Windows 2000. Characteristics of this environment are that output appears in a pop-up window, and script execution waits until the user clears the pop-up window. The other environment is the CScript environment. As you might guess, the primary difference between the CScript and WScript environments is that the CScript environment directs display information to the command window, which has a side effect of letting a script run to completion without pausing for each message sent to the UI.
Sometimes, running a particular script under a different environment is helpful. For example, if there are multiple different output lines, it can become annoying to clear each different message before the script will continue. Conversely, a script with a warning message might run to completion under the CScript environment without ever giving the user a real chance to cancel its execution. Now, you might think that by default, a script that you execute from within a command window would run under the CScript environment. However, only one default setting for each user exists, so Windows uses the default from the registry regardless of where the script is executed. To get around this default, you can preface the call to a given script with a call to the environment in which it should run (e.g., cscript adsutil.vbs).
If you want to quickly execute scripts and don’t want any pop-up windows typing cscript at the start of each command, you can change the default host environment from WScript to CScript. Using either the wscript or cscript command with the //H:cscript option resets the default for the current command window. An example of this reset is what the adsutil.vbs script does if you accidentally attempt to run it in the WScript environment. The script asks your permission to change your session default to the CScript environment so it can execute and display information appropriately.
However, this step still leaves you executing the call each time you log on. To make this change permanent, you need to put a second option (//s) on the line where you set the default host. An example of the entire command is
Cscript //h:cscript //s
You can replace the word cscript either as the command, default environment, or both in the above example (e.g., wscript //h:wscript). (For a complete list of the options you can use with the cscript and wscript commands, use /? while in CScript mode.)
Here’s the catch: When you’ve changed your default, if you accidentally double-click a .vbs file, you’ll briefly see the command window open to display any output. Before you can read the title bar, the script will finish and the window will disappear, along, possibly, with any chance to respond to a warning about deleting those files you’re so fond of keeping on your hard disk.
About the Author
You May Also Like