Windows Scripting Host

By Mayur Bhatia Published Date
01 - Apr - 2007
| Last Updated
01 - Apr - 2007
Windows Scripting Host

More power to the double-click!

Few are aware of scripting with batch files, but people don’t use batch files much unless when working with mountainous tasks. Even fewer are aware of Windows Scripting Host (WSH), a scripting environment that comes integrated with Windows to help automate your tasks, and what’s more, unlike batch scripting, WSH has very few restrictions; you can create scripts to create, edit, and delete text files, Registry entries, create users, change passwords… you name it.

Scripts alone can be so powerful that one can even create viruses using it: the dreaded ILOVEYOU virus is, at its core, just a text file containing a script. Don’t despair—creating and executing scripts in Windows doesn’t require you to install any software or compile anything; instead, it requires code—a lot of code. OK, despair a bit, but in the end it’ll be worth it.

To be clear, first and foremost, this is not a tutorial to WSH itself, but rather a collection of scripts for automating certain tasks. So if anyone wants to jump straight to creating and using the script, you can simply type it in and save it as a file. For those hungry for detail, we’ll be explaining each script as it comes. Hopefully, this should be help enough for you to start writing your own scripts.

About the only thing you will need to create and run the scripts is the trusty Notepad. Enter the script text and save the file with a .vbs extension. Whenever you want to use the script, simply double-click the file to execute your script. That said, let’s get down to the scripts.

Start multiple programs and documents at the same time
This one will allow you to start several programs and open several documents upon opening a single file. You can start any number of programs you want in this manner. Individual files will open as if you’d double-clicked on them. This means the files will open the program associated with them. Remember that this function, at its core, is a simple Run command. Anyhow, here it is:
Set WshShell = WScript.CreateObject(“WScript.Shell”)
WSHShell.Run “Notepad”
WSHShell.Run “C:\Reports\Reports.doc”
WSHShell.Run “C:\Presentation\Presentation.ppt”
WSHShell.Run “C:\Presentation\Presentation.pps”
WSHShell.Run “”
WSHShell.Run “C:\MP3\Dragula.mp3”

Simple, isn’t it? This one uses only a single function—“run”. The line at the top indicates we want access to the Wscript.Shell object. The following lines, like they suggest, tell Windows that we want to use the Run function. Remember that these programs will open in order from top to bottom, and you can enter as many run functions as you want. This means you will be able to open your Word file, your presentation document, Notepad, and also browse to your favourite site, all the while listening to an MP3—all with nothing but a simple double-click of a file. No more hunting around for different icons!

Take care not to make little mistakes when it comes to the spaces, quotes, commas, and dots, though the script text is not case sensitive.

Copy and move files and folders

On we come to the main part. This is what you will find yourself using the most. Performing daily tasks like copying / moving files and folders to and fro from one folder to another can be a painful task. Not anymore, not when you have this script at your disposal:

Set FSO = CreateObject(“Scripting.FileSystemObject”)
FSO.CopyFile “C:\text\*.*”, “C:\text1”
FSO.MoveFile “C:\test\*.*”, “C:\test1”
The first line declares the filesystem object, since we want to make changes to the filesystem itself. FSO.CopyFile and FSO.MoveFile are the functions used to copy and move files respectively. In this case it’s all files in the folders “text” and “test”; the *.* characters indicates we want to copy all files from their specified directories on to the destination, whose path is declared at the end. Again, you can use these commands however many times you want in the script for multiple tasks.

Delete files

You probably saw this coming after the last one: you can use the “deletefile” object to delete any files. This is useful when you want to make sure that certain directories are empty or certain files are deleted before you shut down Windows, or simply to clear out all temporary files. The script:
Set FSO = CreateObject(“Scripting.FileSystemObject”)
FSO.DeleteFile “C:\test1\temp.doc”

Rather self-explanatory. You’re already familiar with the first part by now; the next line has an easy-to-understand function, “FSO.DeleteFile”, followed by the path to the folder or file you want to delete. If you want to nuke the entire directory, use the *.* variable to include all files in the directory specified in the script.

Map a network drive using a shortcut

Ever wanted a simple shortcut to map your network shares to a drive? You can do so using this script. Remember that you can also add as many drives you want to map, similar to WSHShell.Run. Here it is:
Set WSHNetwork = WScript.CreateObject(“WScript.Network”)
WSHNetwork.MapNetworkDrive “S:”, “\\network\share 1”
WSHNetwork.MapNetworkDrive “T:”, “\\network\share 2”
WSHNetwork.MapNetworkDrive “U:”, “\\network 2\share 3”
Again, the first line asks WSH to declare the “WSHNetwork” object. As mentioned before, since we are making changes to the network subsystem—a network drive in this case—we’ll need to declare it, like we have done in the first line. Further down, we see “WSHNetwork” using the “MapNetworkDrive” function, this is the actual command for creating a network drive. Take note that the drives themselves are in their own set of quotes followed by the network shares. Similar to the Run function earlier, the drives will be mapped in order from top to bottom.

Disconnect network drives

Yes, similar to how you can map your network drives together, you can also disconnect any number of network drives you mapped, thus not requiring you to manually do it every time you shut down your computer. Remember, simply because this is the opposite of the last script, it won’t need your drives to have been previously created by WSH. Here’s the script.

Set WshNetwork = WScript.CreateObject(“WScript.Network”)
WshNetwork.RemoveNetworkDrive “S:”, True
WshNetwork.RemoveNetworkDrive “T:”, True

The first line is used to declare that we want to make changes to the network subsystem. This is followed by the function used in the network object, which is to remove a network drive. This function is followed by the network drive you want to disconnect. Enter this command however many times you want to disconnect more drives; just remember to replace the drive letter. Disconnect commands are executed in order from top to bottom, as usual.

The “True” Boolean at the end of the script indicates to WSH to disconnect the drive even if it’s still in use. You can change this Boolean to “False”, or not enter it at all, to prevent WSH from disconnecting drives in use.

Create or edit Registry values

This script will allow you to create one or more Registry values using a single file. This can be useful when dealing with tweaks concerning the registry, which require you to create values. The script:

Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.RegWrite “HKLM\Software\new\text”, “Normal text”, “REG_SZ”
WshShell.RegWrite “HKLM\Software\new\Numbered value”, 543, “REG_DWORD”
As always, the first line declares the object. The function in the next line is RegWrite, which indicates we want to make changes to the Registry. Next comes the path to the values you want to create, followed by the data you want to input. Finally comes the value type: REG_SZ indicates that it be a string value, REG_DWORD indicates a DWORD number (the value will be stored in decimal format), REG_BINARY indicates a binary value, and so on.
Also, don’t be confused by the “HKLM”; this expands to HKEY_LOCAL_MACHINE. For HKEY_CURRENT_USER, you can use HKCU, and so on. Remember that the steps to create and edit values are the same. This script can create a single file full of all your custom tweaks for the Registry with a mere double-click.

Delete Registry keys

Using this script, you can delete multiple registry entries using a single file. This should allow you to delete even entire keys. Here’s the script for it. Enter it in Notepad and save it with the .vbs extension.

Set WSHShell=WScript.CreateObject (“WScript.Shell”)
WSHShell.RegDelete “HKCU\Software\EAG\”
WSHShell.RegDelete “HKCU\Software\EAG\username”

The first line declares it’s going to make changes to the shell. The second line—WSHShell.RegDelete—is self-explanatory and asks WSH to delete a Registry key. The key to be deleted is entered after this command in double quotes. You can add as many entries as you want.

We have added two commands for deleting; this is for the sake of explaining the difference between deleting values (the entries on the right-hand pane in the registry) and keys (the folder-like entries on the left-hand pane). The only difference between the two is that you have to include a “\” at the end of the path when deleting a key. For example, “HKCU\software\value” will remove a value called “value”, while “HKCU\software\value\” will delete a key called “value”.

Open important documents located in network shares

As you probably are now aware of the general format of the scripts, this one is an example of how you can combine two scripts to make tasks easier. This script will first map a network share to a drive and then open a document located on it.

Set WSHNetwork = WScript.CreateObject(“WScript.Network”)
WSHNetwork.MapNetworkDrive “S:”, “\\network\share 1”
WSHShell.Run “S:\reports.doc”
WSHShell.Run “\\network\share 1”

The first line is what we explained earlier, declaring the network object and then including the “MapNetworkDrive” function. The second line should map your network share to a drive letter, and the other lines, containing WSHShell.Run, will open a document located in your network shares. Note that one of the lines containing the Run function has S:, a drive letter that was mapped earlier in the script. Were you to put this line on the top, WSH wouldn’t be able to determine where S: is located. This example is solely to explain that you should indeed plan out your scripts as they occur in order from top to bottom. (Yes, we said that already, but it needed mentioning once more.)

Start the Registry editor without remembering the last used key

How often do you hate it upon starting the Registry editor, that the path you were last working on shows up directly in the window? With this script, you can start regedit without opening the last window used. It works by first removing an entry in the Registry that contains the last-used key path, thus clearing it, and then starting regedit.

Set WSHShell=WScript.CreateObject (“WScript.Shell”)
WSHShell.RegDelete “HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\LastKey”
The first line declares the “WSHShell” object; this is necessary whenever you’re working with scripts that make changes to the Registry or to individual files.
The second line contains the major change, which is to delete the value storing the path to the last-used key; it naturally uses the “regdelete” function, which was explained earlier.

Finally, once the last-used path in the Registry is cleared out, the simple “WSHShell.Run” function explained earlier will start regedit at its root folder.

Mix it up

That’s pretty much enough to get the feel of scripting; you can also combine several scripts together to form a single one, like we’ve seen in the last two examples. Remember that scripts run in order from top to bottom:  So make sure you plan out your scripts properly since the earlier functions won’t be aware of the commands below them and won’t be able to execute if they depend on the functions at the end.

If you wish to go deeper into scripting, check out and search for Windows Scripting Host to get a rather lengthy manual containing all the functions possible. It won’t be long before you become so addicted that you will end up writing lengthy scripts for simple tasks instead of executing them normally—just for the sake of it!

Mayur BhatiaMayur Bhatia