Creating a Subkey

Microsoft® Windows® 2000 Scripting Guide

You rarely need to create a registry subkey. However, in some circumstances the solution to a problem - as described in a Knowledge Base article or a security bulletin - requires the addition of a subkey. If you need to create a subkey on a large number of computers, the best solution might very well be a script. After all, a single script can be used to create the subkey on every computer affected by the problem.

The Registry Provider's CreateKey method enables you to add a new subkey. It takes two parameters: a constant indicating the subtree to which the new subkey will be added and the path of the new subkey.

Scripting Steps

Listing 16.8 contains a script that creates a subkey in the SOFTWARE key under the subtree HKEY_LOCAL_MACHINE in the registry. To carry out this task, the script must perform the following steps:

  1. Create a constant to hold the hexadecimal number corresponding to the HKEY_LOCAL_MACHINE registry subtree.

  2. Create a variable and set it to computer name.

  3. Use a GetObject call to connect to the WMI namespace root\default, and set the impersonation level to "impersonate."

  4. Create a variable and set it to the path of the registry subkey to create.

  5. Use the CreateKey method to create the new subkey.

Listing 16.8 Creating a Subkey

  
1
2
3
4
5
6
7
8
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
 strComputer & "\root\default:StdRegProv")
  
strKeyPath = "SOFTWARE\System Admin Scripting Guide"
objReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath

Figure 16.4 shows the newly created subkey in a registry editor.

Figure 16.4 Viewing a Newly Created Subkey Using a Registry Editor

sas_reg_040s