Working with the Windows registry

The primary goal of this lecture is to discuss how WSH works with the WIndows registry. We will not describe what we can do using the registry. To learn this we refer to the Windows Registry Guide, to a short CHM file from the site above, and other resources.

The Windows registry consists of 5 sections. These sections, their short descriptions, and some aliases are summarized in the table below.
Short name Full name Description
HKCU HKEY_CURRENT_USER Controls configuration settings for the current user.
HKLM HKEY_LOCAL_MACHINE Controls system-level configuration settings.
HKCR HKEY_CLASSES_ROOT Configuration settings for applications and files. Ensures the correct application is opened when a file is started through WIndows Explorer or OLE.
  HKEY_USERS Stores default-user and other-user settings by profile.
  HKEY_CURRENT_CONFIG   Contains information about the hardware profile being used.
For example, the HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main section stores information about the Internet Explorer. The keys stored in this section defines different things about IE behavior. For instance, the key Start Page contains the start page of the IE.

The values of registry keys can have one of the following types:

Dealing with registry keys

The WshShell object provides three methods to deal with registry keys and values:

RegRead Method

This method returns the value of a key or value-name from the registry. The general syntax is
RegRead(keyName)
where keyName is a string value indicating the key or value-name whose value you want. The RegRead method returns values of the following five types.
Type Description In the Form of
REG_SZ A string A string
REG_DWORD A number An integer
REG_BINARY A binary value A VBArray of integers
REG_EXPAND_SZ An expandable string (e.g., "%windir%\\calc.exe") A string
REG_MULTI_SZ An array of strings A VBArray of strings
You can specify a key-name by ending keyName with a final backslash. Do not include a final backslash to specify a value-name. A value entry has three parts: its name, its data type, and its value. When you specify a key-name (as opposed to a value-name), RegRead returns the default value. To read a key's default value, specify the name of the key itself. Fully qualified key-names and value-names begin with a root key. You may use abbreviated versions of root key names with the RegRead method.

RegWrite Method

This method creates a new key, adds another value-name to an existing key (and assigns it a value), or changes the value of an existing value-name. The general syntax is
RegWrite(keyName, anyValue [,type])
where Specify a key-name by ending keyName with a final backslash. Do not include a final backslash to specify a value name. The RegWrite method automatically converts the parameter anyValue to either a string or an integer. The value of type determines its data type (either a string or an integer). The options for type are the types listed above.

Note The REG_MULTI_SZ type is not supported for the RegWrite method.
Note RegWrite will write at most one DWORD to a REG_BINARY value. Larger values are not supported with this method. Fully qualified key-names and value-names are prefixed with a root key. You may use abbreviated versions of root key names with the RegWrite method.

RegDelete Method

This method deletes a key or one of its values from the registry.
RegDelete(keyName)
where keyName is a string value indicating the name of the registry key or key value you want to delete.

Specify a key-name by ending keyName with a final backslash; leave it off to specify a value-name. Fully qualified key-names and value-names are prefixed with a root key. You may use abbreviated versions of root key names with the RegDelete method.

The following example shows how to check if your IE browser has your favorite page set as a start page and sets it if it's not.

var page = "http://www.google.com";
var msg1 = "You start up page is '";
var msg2 = "'. Do you want to reset your favorite page?";
var title = "Start page has been changed";
var path = "HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\";
var key  = "Start Page";
var shell = WScript.CreateObject("WScript.Shell");
try{
  var spage = shell.RegRead(path+key);
  if( spage != page ) 
    if( shell.Popup(msg1+spage+msg2, 0, title, 4+32) == 6  ){
      shell.RegWrite(path+key, page);
      WScript.Echo("Your start page is reset to '"+page+"'");
  }
}
catch( e ){
   var code = e.number & 0xFFFF;
   var msg = "Error " + code + ": " + e.description;
   shell.Popup(msg, 0, "Run-time error in the script "+WScript.ScriptName, 16);
}

The following script sets the auto-logon option to ON.

var AutoAdminLogon, DefaultUserName, DefaultPassword, DontDisplayLastUserName;
var logon = "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\";

// read the current values
try{ AutoAdminLogon = shell.RegRead(logon+"AutoAdminLogon"); }
catch(e){  AutoAdminLogon = "--no value--"; }

try{  DefaultUserName = shell.RegRead(logon+"DefaultUserName"); }
catch(e){  DefaultUserName = "--no value--"; }

try{ DefaultPassword = shell.RegRead(logon+"DefaultPassword"); }
catch(e){  DefaultPassword = "--no value--"; }

try{ DontDisplayLastUserName = shell.RegRead(logon+"DontDisplayLastUserName"); }
catch(e){  DontDisplayLastUserName = "--no value--"; }

str = "AutoAdminLogon: "+AutoAdminLogon+"\n"+
      "DefaultUserName: "+DefaultUserName+"\n"+
      "DefaultPassword: "+DefaultPassword+"\n"+
      "DontDisplayLastUserName: "+DontDisplayLastUserName+"\n";
      
 WScript.Echo(str);
 
try{ 
  // set autologon
  shell.RegWrite(logon+"AutoAdminLogon", "1", "REG_SZ");
  shell.RegWrite(logon+"DefaultUserName", "administrator", "REG_SZ");
  shell.RegWrite(logon+"DefaultPassword", "test", "REG_SZ");
  shell.RegWrite(logon+"DontDisplayLastUserName", "0", "REG_SZ");
 }
 catch( e ){
   var code = e.number & 0xFFFF;
   var msg = "Error " + code + ": " + e.description;
   shell.Popup(msg, 0, "Run-time error in the script "+WScript.ScriptName, 16);
}


References: