Recipe 9.2. Setting a ValueProblemYou want to create, modify, or delete a registry value. SolutionUsing a graphical user interface
Using a command-line interfaceThe following command sets a registry value: > reg add \\<ServerName>\<Key> /v <ValueName> /t <ValueType> /d <ValueData> For example: > reg add \\fs01\HKLM\Software\Rallencorp /v Version /t REG_SZ /d "1.2" > reg add \\.\HKLM\Software\Rallencorp /v Setting1 /t REG_DWORD /d 1024 One nice thing about the reg add command is that it automatically creates the Rallencorp subkey if it doesn't already exist. This command deletes a registry value: > reg delete \\<ServerName>\<Key> /v <ValueName> For example: > reg delete \\fs01\HKLM\Software\Rallencorp /v Version Using VBScript
' This code sets a registry string value ' ------ SCRIPT CONFIGURATION ------ const HKLM = &H80000002 strKeyPath = "<RegKey>" ' e.g., Software\Rallencorp strStringValueName = "<ValueName>" ' e.g., Version strStringValue = "<ValueData>" ' e.g., 1.2 strComputer = "<ServerName>" ' e.g., server01 (use "." for local server) ' ------ END CONFIGURATION --------- set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") intRC = objReg.SetStringValue(HKLM, strKeyPath, strStringValueName, _ strStringValue) if intRC <> 0 then WScript.Echo "Error setting value: " & intRC else WScript.Echo "Successfully set value: " & strStringValueName end if ' This code deletes a registry value ' ------ SCRIPT CONFIGURATION ------ const HKLM = &H80000002 strKeyPath = "<RegKey>" ' e.g., Software\Rallencorp strStringValueName = "<ValueName>" ' e.g., Version strComputer = "<ServerName>" ' e.g., server01 (use "." for local server) ' ------ END CONFIGURATION --------- set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") intRC = objReg.DeleteValue(HKLM, strKeyPath, strStringValueName) if intRC <> 0 then WScript.Echo "Error deleting value: " & intRC else WScript.Echo "Successfully deleted value: " & strStringValueName end if DiscussionRegistry keys are used to structure the registry. Registry values are to files what registry keys are to folders. This simple analogy helps describe the purpose of registry values: to store data. Values are made up of three elements: value name, value datatype, and value data. The datatype defines the type of data the value can contain. There are 11 total datatypes; I list the six most common in Table 9-3. If you want to see the more obscure datatypes, see MS KB 256986.
See AlsoRecipe 9.3 and MS KB 256986 (Description of the Microsoft Windows Registry) |