Recipe9.1.Creating and Deleting a Key


Recipe 9.1. Creating and Deleting a Key

Problem

You want to create or delete a registry key.

Solution

Using a graphical user interface

  1. Open the Registry Editor (regedit.exe).

  2. Browse to the location where you want to create or delete a key.

  3. To create a key: Right-click the parent key, select New

    To delete a key: Right-click the key you want to delete, select Delete, and click Yes to confirm.

Using a command-line interface

The following command creates a registry key in the HKLM hive called Rallencorp:

> reg add \\<ServerName>\HKLM\Software\Rallencorp

The following command deletes the same registry key:

> reg delete \\<ServerName>\HKLM\Software\Rallencorp

You will be prompted to confirm the deletion. Use the /f option to delete the key and bypass the confirmation prompt.

If you have a registry file (i.e., a file with a .reg extension), you can also import it using regedit:

> regedit /s <Filename>

The /s option suppresses all windows and dialog boxes. See MS KB 310516 for more on creating registry files.

Using VBScript
' This code creates a registry key. ' ------ SCRIPT CONFIGURATION ------ const HKLM = &H80000002 strKeyPath = "<RegKey>"      ' e.g., Software\Rallencorp strComputer = "<ServerName>" ' e.g., serv01 (use "." for local server) ' ------ END CONFIGURATION --------- set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") intRC = objReg.CreateKey(HKLM, strKeyPath) if intRC <> 0 then    WScript.Echo "Error creating key: " & intRC else    WScript.Echo "Successfully created key " & strKeyPath end if ' This code deletes a registry key. ' ------ SCRIPT CONFIGURATION ------ const HKLM = &H80000002 strKeyPath = "<RegKey>"      ' e.g., Software\Rallencorp strComputer = "<ServerName>" ' e.g., server01 (use "." for local server) ' ------ END CONFIGURATION --------- set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") intRC = objReg.DeleteKey(HKLM, strKeyPath) if intRC <> 0 then    WScript.Echo "Error deleting key: " & intRC else    WScript.Echo "Successfully deleted key " & strKeyPath end if

Discussion

A registry key is nothing more than a container of other keys and registry values. There are six root keys that are used to logically group similar registry data. Table 9-2 lists each of the root keys and describes their purpose.

Table 9-2. The six root keys of the registry

Root key

Description

HKEY_LOCAL_MACHINE

This is the most important root key. It is where most system and application configuration data is stored. It is abbreviated HKLM.

HKEY_CURRENT_USER

This is actually a link to the subkey under HKEY_USERS for the currently logged on user. This is useful because it allows applications to access a single registry path to get any configuration information for the currently logged on user. It is abbreviated HKCU.

HKEY_CURRENT_CONFIG

This is also a link that points under HKLM to hardware information for the current hardware profile. Since you can have different hardware profiles (as you can user profiles), this allows applications to access the one that is currently in use. It is abbreviated HKCC.

HKEY_CLASSES_ROOT

The subkeys under this key map file extensions to the applications that own them. It is abbreviated HKCR and is a link to HKLM\Software\Classes.

HKEY_PERFORMANCE_DATA

This key is used by applications that want to access performance data. It doesn't actually store the performance data, but serves as an interface to the data. It is abbreviated HKPD.

HKEY_USERS

This key stores profile information for all users of the system. It contains things such as environment variable values and user-specific customization settings. It is abbreviated HKU.


See Also

Recipe 9.2 for setting a value, MS KB 310516 (How to back up, edit, and restore the registry in Windows XP and Windows Server 2003), MS KB 82821 (Registration Info Editor (REGEDIT) Command-Line Switches), and MS KB 310516 (How To Add, Modify, or Delete Registry Subkeys and Values by Using a Registration Entries (.reg) File)



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net