What is WSH?OverviewWindows Script, also called Windows Script Host (WSH), allows you to write scripts to automate tasks on a Windows computer much like Unix shell scripts do in a Unix environment. WSH is much more powerful than the batch file programs that used to be used in DOS. Some version of WSH is included in all recent versions of Windows. WSH includes both VBScript and JScript scripting engines. These web pages only discuss using VBScript. Objects are used extensively in WSH. A Simple ExampleThe following is a very simple WSH script written in the VBScript language. Copy these lines of code into Notepad or another text editor and save in a file named hello.vbs. Note that the vbs extension identifies the language used as VBScript.
The lines that start with a single quote are comments, of course. The 2 lines of actual code both use the Echo method (function) on the WScript object. In each case, a message is displayed on the screen. Running a WSH ScriptThere are several ways of running a script such as the hello.vbs script. You can simply double click on the hello.vbs file in My Computer. This runs the program in the Wscript GUI-based utility. Each of the above 2 messages is displayed in a dialog window on the screen. You have to click on the OK button after reading each message. If you go to a command prompt you can run your script with either the Wscript utility or the command-line Cscript utility. For example, to run the above script under the Cscript utility, enter:
Each message is simply displayed on a new line in the DOS box. There is
no OK button on which to click. You can also run your script using Wscript
by entering the following at the command line:
The result is the same as when double-clicking on the script file in My
Computer. With scripts that use commands to send output to a dialog
box, the output will obviously be in a dialog box whether Cscript or
Wscript is used. It is with echo that the output appears differently.
(Msgbox and Popup are two ways to place output in a dialog box. Msgbox
is described in the Reporting on Network Drives
section and Popup in the
Another Way to Report on Network Drives section.)
Note that scripts can also be run at set times by using the Windows Task Scheduler (at least, if it makes sense to run the script automatically at a set time). The Task Scheduler can usually be found under Accessories, System Tools, Scheduled Tasks. Back to main Windows Script Host page |