Real Visual Basic veterans might remember how to code clever little DOS batch programs that would automate your PC. Before Windows (Can anybody remember that now?) there were whole books written about DOS batch files because they were simple and anyone could whip out one of these little text files with Edit. (Edit is what programmers used before NotePad and it's still available if you want to try it. Just enter "Edit" at a DOS command prompt.)
You weren't any kind of techie unless you had written your own batch file to start your favorite programs from a DOS menu. "Automenu" was one of those kitchen table startup companies back then. Knowing that we could get excited over - "Gee Whiz" - the ability to start programs from a menu should help you understand why Windows was so revolutionary.
But in fact, the early versions of Windows took a step backward precisely because they didn't give us a "Windows" way to create this type of desktop automation. We still had batch files - if we were willing to ignore Windows. But if we wanted to use Windows, the joy of writing a simple piece of code that made your computer more personal just wasn't there.
All that changed when Microsoft released WSH - Windows Script Host. It's a lot more than just a way to write simple programs. This short tutorial will show you how to use WSH, and we'll dig into how WSH is much, much more than DOS batch files ever dreamed of being by showing how to use WSH for hard-core computer administration.01of 05
Microsoft/Wikimedia Commons/Public Domain
If you're just learning about VBScript, it can be kind of confusing to figure out where it "fits in" in the Microsoft world. For one thing, Microsoft currently offers three different 'host' for VBScript.
- Internet Explorer (IE)
- Internet Information Server (IIS)
- Windows Script Host (WSH)
Since VBScript is interpreted, there must be another program that provides the interpretation service for it. With VBScript, this program is called the 'host'. So, technically, VBScript is three different languages because what it can do depends entirely on what the host supports. (Microsoft makes sure that they are virtually identical, however.) WSH is the host for VBScript that works directly in Windows.
… you use the statement…
… and then code your program in VBScript. This is only a good idea if you can guarantee that only IE will be used. And the only time you can do this is usually for a corporate system where only one type of browser is allowed.02of 05
Clearing up Some "Points of Confusion"
Another point of confusion is that there are three versions of WSH and two implementations. Windows 98 and Windows NT 4 implemented version 1.0. Version 2.0 was released with Windows 2000 and the current version is numbered 5.6.
The two implementations are one that works from a DOS command line (called "CScript" for Command Script) and one that works in Windows (called "WScript"). You can use CScript only in a DOS command window, but it's interesting to note that much of the real world computer systems administration still works that way. It might also be confusing to discover that the WScript object is essential to a lot of code that is normally run in CScript. The example shown later uses the WScript object, but you can run it with CScript. Just accept it as maybe being slightly odd, but that's the way it works.
If WSH is installed, you can run a VBScript program by simply double-clicking on any file that has the vbs extension and that file will be executed by WSH. Or, for even more convenience, you can schedule when a script will run with Windows Task Scheduler. In partnership with Task Scheduler, Windows can run WSH and a script automatically. For example, when Windows starts, or every day at a particular time.03of 05
WSH is even more powerful when you use objects for things like managing a network or updating the registry.
On the next page, you'll see a short example of a WSH script (adapted from one supplied by Microsoft) that uses WSH to create a desktop shortcut to the Office program, Excel. (There are certainly easier ways to do this - we're doing it this way to demonstrate scripting.) The object this script uses is 'Shell'. This object is useful when you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder. This particular piece of code simply creates a desktop shortcut to Excel. To modify it for your own use, create a shortcut to some other program you want to run. Note that the script also shows you how to set all the parameters of the desktop shortcut.04of 05
The Example Code
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop _
oShellLink.TargetPath = _
"C:Program FilesMicrosoft OfficeOFFICE11EXCEL.EXE"
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+F"
oShellLink.IconLocation = _
"C:Program FilesMicrosoft OfficeOFFICE11EXCEL.EXE, 0"
oShellLink.Description = "My Excel Shortcut"
oShellLink.WorkingDirectory = strDesktop
Running the Example… and What's Next
To try out this script, simply copy and paste it into Notepad. Then save it using any name… such as "CreateLink.vbs". Remember that Notepad will add ".txt" to files automatically in some cases and the file extension must be ".vbs" instead. Then double click the file. A shortcut should appear on your desktop. If you do it again, it just recreates the shortcut. You can also start the DOS Command Prompt and navigate to the folder that the script was saved in and run it with the command…
… where "scriptfilename" is replaced with the name you used to save it. See the example shown in the screenshot above.
Give it a try!
One caution: Scripts are used a great deal by viruses to do bad things to your computer. To combat that, your system may have software (such as Norton AntiVirus) that will flash a warning screen when you try to run this script. Just select the option that allows this script to run.
Although using VBScript in this mode is great, the real payoff for most people comes in using it to automate systems like WMI (Windows Management Instrumentation) and ADSI (Active Directory Service Interfaces).