Recipe 9.1. Creating and Deleting a KeyProblemYou want to create or delete a registry key. SolutionUsing a graphical user interface
Using a command-line interfaceThe 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 DiscussionA 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.
See AlsoRecipe 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) |