Previous Page
Next Page

Creating Registry Keys

To create keys and subkeys in the registry, you use the CreateKey method, as illustrated in the CreateRegKey.vbs script:

CreateRegKey.vbs

Option Explicit
On Error Resume Next
Dim strKeyPath 'the portion of registry to read
Dim strComputer 'the target computer
Dim objReg 'holds connection to registry provider
Dim subKey 'used to enumerate throught the array
Dim arrSubKeys 'holds the sub keys
Dim ParentKey
Const HKCR = &H80000000 'HKEY_CLASSES_ROOT
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Const HKU = &H80000003 'HKEY_USERS
Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG

ParentKey = "SOFTWARE\EdWilson"
strKeyPath = "SOFTWARE\EdWilson\VBScriptBook"

strComputer = "."

Set objReg=GetObject("winmgmts:\\" & _
  strComputer & "\root\default:StdRegProv")


objReg.CreateKey HKLM, strKeyPath

  WScript.Echo("Created key :" & strKeyPath)
  WScript.Echo("New subkey under : " & ParentKey)

objReg.EnumKey HKLM, ParentKey, arrSubKeys
For Each subKey In arrSubKeys
  WScript.Echo vbTab & subKey
Next

Header Information

The Header information section is similar to that of ReadHotFixes.vbs. The only new variable is ParentKey, which is used to hold the path to the parent key that gets created.

Reference Information

The Reference information section is where you assign values to the variables defined in the Header information section. You assign a value to ParentKey of SOFTWARE\ EdWilson. You assign the value of SOFTWARE\EdWilson\VBScriptBook to strKeyPath. To create the registry key and subkey, you need only the strKeyPath variable. However, because you intend to use EnumKey to verify that you successfully created the new key and subkey, you defined ParentKey to simplify the use of EnumKey. The remaining items in the Reference information section of the script are the same as in the previous script. The beauty of the StdRegProv class is how similarly you use it through all the different methods. The Reference section of the script is seen below.

Const HKCR = &H80000000 'HKEY_CLASSES_ROOT
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Const HKU = &H80000003 'HKEY_USERS
Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG

ParentKey = "SOFTWARE\EdWilson"
strKeyPath = "SOFTWARE\EdWilson\VBScriptBook"

strComputer = "."
Set objReg=GetObject("winmgmts:\\" &_
  strComputer & "\root\default:StdRegProv")

Worker and Output Information

In the Worker and Output information section of the script, you create the key and subkey and then use EnumKey to verify the existence of the new key. The only difference between using CreateKey and EnumKey is that CreateKey needs only two arguments: the registry tree constant and the key path to create. EnumKey, on the other hand, uses three arguments: the registry tree constant, the key path to enumerate, and the variable to hold the output. The Worker and Output section of the script is seen below.

objReg.CreateKey HKLM, strKeyPath

WScript.Echo("Created key :" & strKeyPath)
WScript.Echo("New subkey under : " & ParentKey)

objReg.EnumKey HKLM, ParentKey, arrSubKeys
For Each subKey In arrSubKeys
  WScript.Echo vbTab & subKey
Next


Previous Page
Next Page