The Registry is
one the most crucial data structures in Windows. However, the Registry
isn’t a tool that only Windows yields. Most 32-bit applications make use
of the Registry as a place to store setup options, customization values
the user selected, and much more. Interestingly, your scripts can get
in on the act as well. Not only can your scripts read the current value
of any Registry setting, but they can also use the Registry as a storage
area. This enables you to keep track of user settings, recently used
files, and any other configuration data that you’d like to save between
sessions. This section shows you how to use the WshShell object to manipulate the Registry from within your scripts.
Reading Settings from the Registry
To read any value from the Registry, use the WshShell object’s RegRead method:
WshShell.RegRead(strName)
WshShell | The WshShell object. |
strName | The name of the Registry value or key that you want to read. If strName ends with a backslash (\), RegRead returns the default value for the key; otherwise, RegRead returns the data stored in the value. Note, too, that strName must begin with one of the following root key names: |
| Short Name | Long Name |
| HKCR | HKEY_CLASSES_ROOT |
| HKCU | HKEY_CURRENT_USER |
| HKLM | HKEY_LOCAL_MACHINE |
| N/A | HKEY_USERS |
| N/A | HKEY_CURRENT_CONFIG |
The script in Listing 1 displays the name of the registered owner of this copy of Windows XP.
Listing 1. A Script That Reads the RegisteredOwner Setting from the Registry
Set objWshShell = WScript.CreateObject("WScript.Shell") strSetting = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOwner" strRegisteredUser = objWshShell.RegRead(strSetting) WScript.Echo strRegisteredUser
|
Storing Settings in the Registry
To store a setting in the Registry, use the WshShell object’s RegWrite method:
WshShell.RegWrite strName, anyValue [, strType]
WshShell | The WshShell object. |
strName | The name of the Registry value or key that you want to set. If strName ends with a backslash (\), RegWrite sets the default value for the key; otherwise, RegWrite sets the data for the value. strName must begin with one of the root key names detailed in the RegRead method. |
anyValue | The value to be stored. |
strType | The data type of the value, which must be one of the following: REG_SZ (the default), REG_EXPAND_SZ, REG_DWORD, or REG_BINARY. |
The following statements create a new key named ScriptSettings in the HKEY_CURRENT_USER root:
Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.RegWrite "HKCU\ScriptSettings\", ""
The following statements create a new value named NumberOfReboots in the HKEY_CURRENT_USER\ScriptSettings key, and set this value to 1:
Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.RegWrite "HKCU\ScriptSettings\NumberOfReboots", 1, "REG_DWORD"
Deleting Settings from the Registry
If you no longer need to track a particular key or value setting, use the RegDelete method to remove the setting from the Registry:
WshShell.RegDelete(strName)
WshShell | The WshShell object. |
strName | The name of the Registry value or key that you want to delete. If strName ends with a backslash (\), RegDeleteRegDelete deletes the value. strName must begin with one of the root key names detailed in the RegRead method. deletes the key; otherwise, |
To delete the NumberOfReboots value used in the previous example, you would use the following statements:
Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.RegDelete "HKCU\ScriptSettings\NumberOfReboots"