Reading From and Writing to the Local Registry

Microsoft® Windows® 2000 Scripting Guide

As a general rule, it is best to manage the registry using system tools such as Regedit.exe; although not foolproof, these tools have built-in safeguards that help minimize the damage that can be caused by incorrectly configuring a registry entry. On the other hand, it is also true that many of these registry tools cannot be automated and are designed to work on only one computer at a time (typically the local computer). It is one thing to say that you should use Regedit.exe to manage the registry; it is quite another to have an urgent security bulletin recommending that you change a registry entry on all 1,000 of your domain controllers as quickly as possible. In situations in which system tools are not fast enough or efficient enough, the WshShell object provides methods for reading from, writing to, and deleting from the registry.

Caution

  • Changing the registry with a script can easily propagate errors. The scripting tools bypass safeguards, allowing settings that can damage your system, or even require you to reinstall Windows. Before scripting changes to the registry, test your script thoroughly and back up the registry on every computer on which you will make changes. For more information about scripting changes to the registry, see the Registry Reference on the Microsoft Windows 2000 Server Resource Kit companion CD or at Microsoft Windows 2000 Server Resource Kit.

Reading a Registry Entry

The registry is the primary configuration database for the Windows operating system; the ability of an operating system component to run, and to run correctly, often depends on the configuration of one or more settings within the registry.

As a system administrator, you spend a considerable amount of time checking values set within the registry. For example, in the event of computer problems, support personnel will often ask you to verify specific registry settings. This can be done directly, using a tool such as Regedit.exe, or it can be done programmatically, using the WshShell RegRead method.

For the most part, the RegRead method requires you to do just two things: 1) Create an instance of the WScript Shell object and 2) call the RegRead method, specifying the registry entry you wand to read. For example, the version number of the Windows operating system is stored in HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion. You can retrieve this value by using the following code:

Set objShell = WScript.CreateObject("WScript.Shell")
sngVersion = objShell.RegRead _
    ("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion")
Wscript.Echo sngVersion

Registry Data Types

Each value stored in the registry has a particular data type. Table 3.15 lists the subset of registry types that WSH supports and the corresponding VBScript-compatible types into which the RegRead method translates corresponding registry values.

Table 3.15 Registry Data Types and Associated Script Data Types

Name

Data Type

Script Data Type

REG_SZ

String

Converted to String

REG_DWORD

Number

Converted to Integer

REG_BINARY

Binary Value

Converted to VBArray of Integers

REG_EXPAND_SZ

Expandable String

Converted to String

REG_MULTI_SZ

Array of Strings

Converted to VBArray of Strings

The data types listed in Table 3.15 are the ones most commonly used in the registry. If your script attempts to use the RegRead method to retrieve the value of a registry entry with an unsupported data type, the call will result in an error.

Note

  • Unfortunately, WSH does not provide a way for you to verify the data type of a registry entry before you attempt to read it. However, you can use WMI to verify data types.

The script in Listing 3.27 uses the RegRead method to read the value of a multistring registry entry. Because this is a multistring value, the information is returned as an array, and a For Each loop is used to report each item in that array.

Listing 3.27 Reading a Multistring Value from the Registry

  
1
2
3
4
5
6
Set objShell = WScript.CreateObject("WScript.Shell")
arrValues = objShell.RegRead _
 ("HKLM\SYSTEM\CurrentControlSet\Services\EventLog\Security\Sources")
For Each strValue In arrValues
 Wscript.Echo strValue
Next

When the preceding script is run under CScript, output similar to the following is displayed in the command window:

Spooler
Security Account Manager
SC Manager
NetDDE Object
LSA
DS
Security

Creating or Modifying a Registry Entry

Your scripts can use the RegWrite method to create a new registry entry or modify an existing one. The RegWrite method accepts three parameters: the registry entry to create or modify, the value to assign to the entry, and (optionally) the data type of the entry.

The script in Listing 3.28 uses the RegWrite method to create a DWORD entry (and set the value to 56) in the registry.

Listing 3.28 Creating a DWORD Value in the Registry

  
1
2
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.RegWrite "HKCU\TestKey\Version", 56, "REG_DWORD"

Note

  • The WshShell RegWrite method does not support writing the REG_MULTI_SZ data type.

Deleting a Registry Entry

Your scripts can use the RegDelete method to delete registry subkeys or entries. The RegDelete method accepts a single parameter that specifies the subkey or entry to delete. Deleting a subkey deletes all the entries in that subkey.

The script in Listing 3.29 uses the RegDelete method to delete a DWORD value in the registry.

Listing 3.29 Deleting a DWORD Value in the Registry

  
1
2
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.RegDelete "HKCU\TestKey\Version"