1. Technology

Your suggestion is on its way!

An email with a link to:

http://visualbasic.about.com/od/learnvbscript/l/blvbscriptintro.htm

was emailed to:

Thanks for sharing About.com with others!

VBScript - The Administrator's Utility Language
Use VBScript for routine computer tasks
 Join the Discussion
Do you use VBScript for any routine tasks? Tell us about it in the message board.
Discussion Forum
 

The real veterans out there might remember how to code clever little DOS batch programs that you could use to automate your PC. Before Windows 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 (that’s the primitive DOS version of NotePad). You weren’t any kind of techie unless you had written your own batch file that would start all of your favorite programs from a DOS menu with a single keystroke. (If anything, this should help you understand why Windows was so revolutionary.)

The earliest versions of Windows took a step backward because people like you and me didn’t have a built in way to automate Windows like that. The joy of writing a simple piece of code that made your computer more personal was taken away.

All that changed when Microsoft released WSH – Windows Script Host. It’s a lot more than just a way to write simple programs. This article will show you how to use WSH to run some samples that are probably on your hard disk right now and why they work.

Since many of you are probably know all about how to use VBScript in web pages, you might be wondering about the relationship of that VBScript to the one in WSH. Good news! It’s the same thing. Microsoft currently offers three different ‘hosts’ for VBScript:

     Internet Explorer (IE)
     Internet Information Server (IIS)
     Windows Script Host (WSH)

WSH is just the version that works directly in Windows.

In addition to VBScript, WSH will also let you execute JScript and XML encoded scripts natively within the Windows O/S. And speaking of Windows, this table lists the version of Windows Script Host that is installed with Windows.

Host Application 1.0 2.0 5.6
Microsoft Windows 98 X    
Microsoft Windows NT 4 Option Pack X    
Microsoft Windows 2000   X  
Microsoft Windows XP     X

Windows Script Host is built into Microsoft Windows 98, 2000, and Millennium Editions. If you are running Windows 95, you can download WSH 5.6 from Microsoft's scripting page.

And you can upgrade your current engines. The version of WSH in Windows 98, 2000, and ME is either version 1.0 or 2.0. You have to upgrade to version 5.6 to get the new features.

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 run WSH and a script automatically. For example, when Windows starts, or every day at a particular time.

WSH is even more powerful when you use objects for things like managing a network or updating the registry. Microsoft provides a list of the objects here.

Here’s a short example of a WSH script (adapted from the Microsoft page above) that uses an object to create a desktop shortcut. The object is the Windows ‘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 itself. 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.

     set WshShell = WScript.CreateObject("WScript.Shell")
     strDesktop = WshShell.SpecialFolders("Desktop")
     set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
     oShellLink.TargetPath = WScript.ScriptFullName
     oShellLink.WindowStyle = 1
     oShellLink.Hotkey = "CTRL+SHIFT+F"
     oShellLink.IconLocation = "notepad.exe, 0"
     oShellLink.Description = "Shortcut Script"
     oShellLink.WorkingDirectory = strDesktop
     oShellLink.Save

To try out this script, simply save it as a .vbs file and then double click the file. A shortcut should appear on your desktop. If you do it again, well, it just recreates itself again. Give it a try!

©2014 About.com. All rights reserved.