Menu
WSH to Master Your Computer


Learn more at amazon.com

Windows users interface with their computer through a graphical environment. Even system administrators interface with computers through a graphical environment. But it wasn't always that way. In the early days of computers, system administrators performed their tasks by typing DOS commands. They also wrote scripts, called "batch files", to perform system administration tasks. Even regular users had to know something about DOS in order to use a computer.

Unix and Linux systems also began as command line environments. Even today, Unix and Linux system administrators have a powerful command line environment that gives them capabilities well beyond that of a Windows system administrator.

Whereas Unix and Linux started out as multi-tasking operating systems, Windows started out as a single-task operating system. Although Windows versions 3.1 and earlier appeared to multi-task, they actually ran DOS programs cooperatively. Microsoft was embarassed about the fact that Windows was actually DOS in disguise. They could have developed a multi-tasking DOS, but that was contrary to Microsofts marketing strategy.

Whereas Unix and Linux system administrators could perform powerful tasks through a multi-tasking command-line environment, Windows system administrators had to use a convoluted combination of "Microsoft Management Consoles", Control Panel utilities, and DOS commands to perform tasks.

In the early days, computers had limited speed and capabilities. In order to get programs to run at a reasonable speed, they had to be "compiled" into machine code. Machine code is basically the binary ones and zeros that computers understand.

Today's computers are much more powerful. Much of today's programming is written as scripts. Scripts are text files. When we execute a script, instead of giving the computer Machine code, we pass the script to an "interpreter" program that creates the machine code on the fly. The most common use of scripts is on webpages. The script code can be typed directly into the webpage and then can be executed, or interpreted, any time after the webpage is loaded.

With Windows 2000 and higher, Microsoft included a program called "Windows Scripting Host" (WSH) that allows scripts to be run on a computer outside of a webpage. In effect, DOS batch files have returned, except scripts are multi-tasking and have a lot more power.

Scripts are simply text files. To create them all you need is a simple text editor like Windows Notepad. You have to choose which language you will use to write your script. You can use any language for which an interpreter has been installed. Interpreters for VBScript and Java Script (called JScript on Windows) are installed by default.

VBScript is the easiest language to learn, but it's Microsoft proprietary. If VBScript is all you know, outside of Windows and Internet Explorer you will be programming illiterate. Java Script has a terse syntax similar to C++ (but without the problem causing pointers). People from outside the Windows world prefer to use Java Script because it works virtually everywhere.

If you write in VBScript, you save the file with a ".vbs" extension. If you write in Java Script, you save the file with a ".js" extension. When you double-click on a filename with a .vbs or .js file extension, the WSH creates a "script object" and passes the script to the appropriate interpreter based on the file extension.

In the script code, you can use the methods and properties of the script object. The method you will most commonly use is "CreateObject". For example, you might create a "Network" object. You could then use methods and properties of the Network object to perform administrative tasks across the network. Or you might create a "Shell" object to perform administrative tasks on your local computer.

One of the most common uses of the Shell object would be to read and write Windows registry values. The registry is a database that controls virtually every aspect of a Windows computer. Familiarity with the registry can give you real control of your computer. Of course, you could use the Registry Editor to perform the same task, but then you are dealing with the registry as a whole, exposing yourself to the risk of making an error that causes your computer to fail to start.

Using the WSH, you can deal with one registry key or one value at a time, reducing the chance of making an error. Also, the Internet is full of free, prewritten and tested scripts to perform very specific tasks. Windows Scripts allow you to examine registry keys and explore the registry with minimal risk.

Your First Script

The first script you should write, appropriately, is a script to determine the WSH version installed on your computer. Type or paste the code shown below into Notepad and save the file with the name version.vbs.

MsgBox( _
"Windows Scripting Host Version = " & WScript.Version  & _
VbCrLf & "Visual Basic Script Version = " & _
ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion) 

This script is written in VBScript. VBScript is an interpreter included with the WSH. As you can see, it's very simple. It creates a popup message box. Everything within the parenthesis will be displayed in the message box. VbCrlf is a VBScript constant the creates a carriage return line feed. The ampersand (&) is used to concatinate the text and program constants into a single text string. The underscore (_) is required to tell VBScript that the code is continued on the next line.

When you double-click on the file name, the version of the WSH and VBScript installed on your computer appears in a message box. The latest version is 5.6. What version do you need? When I run this script on my Windows 98 machine, it returns 5.0. You can download the lastest version of the WSH from http://msdn.microsoft.com/scripting, but unless you need it for a specific script, you are probably okay with the version you have.

With DOS we can display and change the computers environmental variables. Environmental variables are a set of variables maintained by the system. For example, the PATH variable contains the default file path, TEMP contains the location of the temp folder. We can also use the WSH to display and change the computers environmental variables. The code below displays a couple of environmental variables that we can't change.

Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
MsgBox( "Number of Processors = " & _
WshSysEnv("NUMBER_OF_PROCESSORS") & VbCrLf & _
"Processor Architecture = " & _
WshSysEnv("PROCESSOR_ARCHITECTURE") & VbCrLf & _
"Processor Identifier = " & WshSysEnv("PROCESSOR_IDENTIFIER"))

Type or paste the code shown above into Notepad and save the file with the name sysinfo.vbs. When you double click on the file name, the number of processor, processor architecture, and processor identifier of your computer appears in a message box.

Having the information returned by a script appear in a popup message box is easy and convenient, but you can also write it to a file. Type or paste the code shown below into Notepad and save the file with the name compname.vbs.

Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("C:\articlework\winscript\
  My_Scripts\compname.txt", True)
Set wshShell = Wscript.CreateObject("Wscript.Shell")
f1.WriteLine "The NetBIOS computer name is:"
f1.WriteLine WshShell.RegRead("HKEY_LOCAL_MACHINE\
  SYSTEM\CurrentControlSet\Control\" & _
"ComputerName\ComputerName\ComputerName")
f1.Close

This script reads your computer's name from the registry, then writes it to a file named compname.txt. You can then open the file in Windows Notepad to read your computer's name. Or add the line shown below to the bottom of the script to automatically run notepad to display the contents of the file.


WshShell.Run wshShell.ExpandEnvironmentStrings("
%windir%\notepad.exe compname.txt")

When this statement executes, the characters %windir% will be replaced with the location of your Windows directory, usually C:/Windows or c:/WINNT, but it could be anything.

Rather than using Regedit and risk corrupting your registry, or trying to figure out where Microsoft hid the utility to perform a task, it's easier to just write a quick script to do the job. Until you become more proficient at writing scripts, you can find thousands of free prewritten and tested scripts to on the Web. You can start building your own collection of scripts.

The Registry

Physically, the registry consists of several files, ntuser.dat and usrclass.dat being two of them, but we don't care about the physical structure of the registry. With scripts, we work with the logical structure of the registry. Logically, the registry is a single database consisting of "keys" and "values". Basically, a key is a "place" to store a value. A key may store many values, or it may have subkeys, each which store values.

The Windows 2000 registry has five top level keys as listed below.

HKEY_LOCAL_MACHINE
HKEY_CURRENT_CONFIG
HKEY_CLASSES_ROOT
HKEY_USERS
HKEY_CURRENT_USER

HKEY_CLASSES_ROOT actually points to information in HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER. HKEY_CURRENT_USER points to information in HKEY_USERS, and HKEY_CURRENT_CONFIG points to information in HKEY_LOCAL_MACHINE. What it boils down to is that HKEY_LOCAL_MACHINE and HKEY_USERS contain the entire registry.

Why have keys that are nothing more than pointers to information in other keys? Because Windows 2000 is a multi-user system, and rather than have the operating system (or our scripts) search for the current user in HKEY_LOCAL_MACHINE and HKEY_USERS, it gets broken out into HKEY_CURRENT_CONFIG and HKEY_CURRENT_USER.

When writing scripts, we can use abbreviations for the names of the top level keys. The abbreviation for HKEY_CURRENT_USER is HKCU. HKCU contains settings for whoever is currently logged on. For example, type or paste the code shown below into Windows Notepad and save the file as iestartpg.vbs.

Set wshShell = Wscript.CreateObject("Wscript.Shell")
MsgBox("Internet Explorer Start Page = " & _
WshShell.RegRead( _
"HKCU\Software\Microsoft\Internet Explorer\Main\Start Page"))

When you double-click on the file name to execute this script, a message box will popup to display Internet Explorer's start page (for the currently logged in user).

Similarly, the script shown below will display Internet Explorers currently configured search page, usually microsoft.com/isapi/redir.dll?prd=ir&iesearch by default.

Set wshShell = Wscript.CreateObject("Wscript.Shell")
MsgBox("Internet Explorer Search Page = " & _
WshShell.RegRead( _
"HKCU\Software\Microsoft\Internet Explorer\Main\Search Page"))

The script shown below displays Internet Explorers currently configured menu options.


Set wshShell = Wscript.CreateObject("Wscript.Shell")
MsgBox("Internet Explorer Toolbar Configuration" & _
VbCrLf & "Show ToolBar: " & WshShell.RegRead( _
"HKCU\Software\Microsoft\Internet Explorer\
   Main\Show_ToolBar") _
& VbCrLf & "Show URL ToolBar: " & WshShell.RegRead( _
"HKCU\Software\Microsoft\Internet Explorer\
   Main\Show_URLToolBar") _
 & VbCrLf & _ "Show Go Button: " & WshShell.RegRead( _
"HKCU\Software\Microsoft\Internet Explorer\
   Main\ShowGoButton") _
& VbCrLf & _ "Show StatusBar: " & WshShell.RegRead( _
"HKCU\Software\Microsoft\Internet Explorer\
   Main\Show_StatusBar") _
& VbCrLf & "Show URL in StatusBar: " & WshShell.RegRead( _ 
"HKCU\Software\Microsoft\Internet Explorer\
   Main\Show_URLinStatusBar"))

Writing to the Registry

Whereas reading the registry is relatively safe, writing to the registry, if not done correctly, can cause your computer to fail to start. In light of that, here's the standard disclaimer:

The script in this article is provided AS IS without warranty of any kind. Bucaro TecHelp disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use of these scripts remains with you. To be safe, you should make a backup copy of your computers registry before using these with scripts. In no event shall Bucaro TecHelp be liable for any damages whatsoever arising out of the use of or inability to use these scripts.

To write to the registry, we will use the "RegWrite" method of the WshShell object, the format of the RegWrite command is shown below.

WshShell.RegWrite(strKey, strValue, strDataType)

"strKey" is a string containing the name of the key to which you want to write. "strValue" is the value you want to write to that key, and "strDataType" is data type of the value you want to write. The possible data types are listed below.

REG_SZ		A string
REG_EXPAND_SZ 	A string where environmental variables are expanded
REG_DWORD	A number in hexidecimal format
REG_BINARY	A number in binary format

For the first example, type or paste the code shown below into Windows Notepad and save the file as writeiepage.vbs.

Dim WshShell, RegKey
Set WshShell = CreateObject("WScript.Shell")
RegKey = "HKCU\Software\Microsoft\Internet Explorer\
  Main\Start Page"
WshShell.RegWrite regkey , "http://bucarotechelp.com/"

When you double-click on the file name to execute this script, your Internet Explorer start page will be changed to bucarotechelp.com.

Note that we assigned the name of the key to the variable "RegKey" and then used the variable as an argument in the "RegWrite" method. We didn't need to use the "strDataType" argument because we used the default data type, REG_SZ.

The script shown below will hide the "GO" button on Internet Explorer's address toolbar.

If MsgBox("Hide Toolbar?", vbQuestion + vbYesNo,
  "IE Config") = vbYes Then
Dim WshShell, RegKey
Set WshShell = CreateObject("WScript.Shell")
RegKey = "HKCU\Software\Microsoft\Internet Explorer\
  Main\ShowGoButton"
WshShell.RegWrite regkey , "no"
End If

We were a little more polite with this script, using a messagebox to request the user's permission before making the change.

In the script shown below, we were even more polite. The first "If" statement checks for a file which on first execution of the script does not exist, so the code goes to the "else" block which reads the registry key for Internet Explorer's default search URL and saves it to a file. Then it prompts you for a new URL, which it writes to the registry.

Upon subsequent executions, the first "If" statement finds the file and asks you if you want to restore the old URL, If you click on the "Yes" button, the previous URL is written to the registry. If you click on the "No" button, it prompts you for a new URL, which it writes to the registry.

Dim fso, f1, searchPage, newPage, wshShell

Set fso = CreateObject("Scripting.FileSystemObject")
Set wshShell = Wscript.CreateObject("Wscript.Shell")

If (fso.FileExists("C:\articlework\WSH\
      My_Scripts\searchURL.txt")) Then
  Set f1 = fso.OpenTextFile("C:\articlework\WSH\
    My_Scripts\searchURL.txt", 1)
  searchPage = f1.ReadLine
  f1.Close
  If MsgBox("Restore: " & searchPage,
    vbQuestion + vbYesNo, "IE Config") = vbYes Then
    WshShell.RegWrite "HKCU\Software\Microsoft\
      Internet Explorer\Main\Search Page", searchPage
  Else
    newPage = InputBox("Enter New Search URL",
     "IE Search Page", searchPage)
    If newPage <> "" Then
      WshShell.RegWrite "HKCU\Software\Microsoft\
         Internet Explorer\Main\Search Page", newPage  
    End If
  End IF
Else
  Set f1 = fso.CreateTextFile("C:\articlework\WSH\
    My_Scripts\searchURL.txt", True)
  Set wshShell = Wscript.CreateObject("Wscript.Shell")
  searchPage = WshShell.RegRead("HKCU\Software\Microsoft\
     Internet Explorer\Main\Search Page")
  f1.WriteLine searchPage
  f1.Close

  newPage = InputBox("Enter New Search URL",
    "IE Search Page", searchPage)
  If newPage <> "" Then
    WshShell.RegWrite "HKCU\Software\Microsoft\
      Internet Explorer\Main\Search Page", newPage
  End If
End If

Now we have one slight problem with this script, which you can discover by selecting "Run" in the "Start" menu and entering "wscript" in the text box. The "Windows Script Host Settings" dialog box will appear. The checkbox next to "Stop script after specified number of seconds" should be checked, and the seconds box should contain the number 2.

This means that any script you run will close after 2 seconds. We don't want to make that delay much longer because a coding error might make a script get caught in an endless loop, and with today's computers, a lot of damage can be done in 2 seconds. On the other hand, the script to change Internet Explorers default search page is going to need to run for more than 2 seconds, unless you can type a new URL in less than 2 seconds.

The solution is, rather than run the script by double-clicking on it, run it from the "Run" dialog box or a command line, and give it a "timeout option as shown below.

wscript //t:30 writesearch.vbs

This lets the script run for 30 seconds before timing out.

A better option is to create a .wsh file (similar to the old PIF file). Open a text file and enter the text shown below (replace the path with your path and script file name).

[ScriptFile]
Path=C:\My_Scripts\writesearch.vbs

[Options]
Timeout=30

Then save the file with the extension ".wsh". When you double-click on the .wsh file, it will run the script with a 30 second timeout. Using .wsh files, you can set individual timeout periods depending upon the requirements and possible infinite loop dangers of each script, while leaving the default timeout at a safe 2 seconds.

You might be thinking, rather than writing this complicated script, couldn't I just change Internet Explorer's configuration through it's menu? Yes you could. I'm using Internet Explorer's configuration here only as an example, so if you mess it up you can easily use Internet Explorer's menu to set it back the way it was.

Our purpose is gain an understanding of the Registry so later we can change some configurations that are not so easily done through an application menu or a control panel utility.


Learn more at amazon.com

More Windows Administration Information:
• Windows Server 2003 Active Directory and Network Infrastructure
• Script to Display the Processes Running on a Computer
• Create a Windows 7 Disk Image Backup
• Internet Connection Sharing in Windows XP
• Three Important Techniques for Securing a Wireless Network
• Disable Annoying 'Get Windows 10' Message
• Turn Off Windows XP Service Pack 2 Firewall
• Top Ten RAID Tips
• Configuring Windows as a NTP (Network Time Protocol) Server
• How to Configure Hyper-V on Windows Server 2019