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 Key, type the name of the key, and hit Enter.

  4. 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 ...

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.