VBScript - The System Administrator's Language - Part 1

01
of 06

Introducing VBScript

VBScript

Real About 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.

02
of 06

VBScript "Hosts"

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 might be familiar with using VBScript in Internet Explorer. Although nearly all HTML on the web uses Javascript since VBScript is only supported by IE, the use if VBScript in IE is just like Javascript except that instead of using the HTML statement ...

SCRIPT language=JavaScript

... you use the statement ...

SCRIPT language=VBScript

... 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.

03
of 06

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.

04
of 06

WSH Objects

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.

05
of 06

The Example Code

~~~~~~~~~~~~~~~~~~~~~~~~~
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop _
  & "\MyExcel.lnk")
oShellLink.TargetPath = _
  "C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE"
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+F"
oShellLink.IconLocation = _
  "C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE, 0"
oShellLink.Description = "My Excel Shortcut"
oShellLink.WorkingDirectory = strDesktop
oShellLink.Save
~~~~~~~~~~~~~~~~~~~~~~~~~

06
of 06

Running the Example ... and what's next

Run VBScript with CScript
Run VBScript with CScript.

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 ...

cscript scriptfilename.vbs

... 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).