Working with the Windows Registry in WSH
(Page 1 of 4 )
One of the most used procedures in Windows Scripting is reading from and writing to the Windows registry. Today we’re going to learn how WSH makes this a very simple task. I’m going to show you ways to do this natively in WSH by using COM objects and I’m also going to show you how to do this by implementing WMI. So hang on, and let’s see how powerful WSH can be.
Introduction
Whether you’re using WSH for system automation or to roll out security policies on several machines, editing the registry will inevitably come up. WSH provides several methods for doing this. We’re going to look in depth at the RegRead, RegWrite, and RegDelete methods and see how to properly implement them in your scripts.
The RegRead, RegWrite, and RegDelete methods belong to the Wscript Shell object. The Shell object is used primarily for reading system information, working with environmental variables, editing the registry, and managing shortcuts. So our first task is to use our script to connect to the Shell object.
Learning the Methods
We create an instance of the shell object by using Wscript’s CreateObject() method. So our first line looks like this:
Set WshShell = WScript.CreateObject("WScript.Shell")
This creates an instance of the Shell object and assigns it to the WshShell variable. ext, let’s learn a little about the methods we discussed earlier.
The RegRead method is used to read values stored in the Windows registry. We provide the registry key and value and the RegRead method returns the data assigned to that value. Here is the syntax for the RegRead method:
object.RegRead Name
Object refers to the variable we’ve assigned to reference the Shell object. In our case, that's WshShell. Name refers to the key, value pair that we are looking for in the registry. When naming registry keys and values in WSH we refer to their hierarchy using the same conventions that we would use to name Windows directory structures. Each of the registry roots can be expressed in full or by using their abbreviated names as shown in the table below.
Table 1: Registry Root Naming Conventions
Abbreviation Root Name
HKCR HKEY_CLASSES_ROOT
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKU HKEY_USERS
HKCC HKEY_CURRENT_CONFIG
The RegWrite method is used to write data to a registry value or to create a registry key. Its syntax is similar to the RegRead method.
object.RegWrite Name, Value [, Type]
Again, object refers to the variable that we’ve assigned to the Wscript Shell object. Name also refers to the registry key or value that we want to work with. Now we have a few added parameters. Value refers to the data that we wish to write in to the specified key or value. The type attribute is optional and is used to define the type of value we wish to create or edit. If you do not specify, the default REG_SZ type is used. Valid options are REG_SZ, REG_EXPAND_SZ, REG_DWORD and REG_BINARY. Value is automatically converted to a string for REG_SZ and REG_EXPAND_SZ and to an integer for REG_DWORD or REG_BINARY.
You can differentiate between registry keys and registry values by including the trailing forward slash (\) for key names.
The RegDelete method is used to remove keys or values from the registry. Its syntax is much simpler than RegWrite. You simply have to specify the key or value to be removed.
object.RegDelete Name \
The syntax for RegDelete is identical to that of RegRead. Now that we have the tools to do the job, let’s see some examples.
Next: Putting it Together >>
More Windows Scripting Articles
More By Nilpo