How Can I Delete a Single Registry Value?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I delete a single registry value? I don’t want to delete the entire registry key, just one value.

— BF

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BF. Registry terminology can be a bit confusing, thanks in large part to those of us here at Microsoft (we tend to use a different set of terms when we talk about the registry than the rest of the world does). So before we answer this question, let’s make sure everyone is clear what we’re talking about. As you can see in the following figure (well, assuming you put this Web page under a microscope; click here if you’d like to see a full-size image), we have a registry key named Test in the left pane and two registry values – Sample Value 1 and Sample Value 2 – in the right pane:

The Windows Registry

You’d like to delete one of these values (say, Sample Value 1) without deleting the other value and without deleting the entire key.

No problem: WMI has a DeleteValue method designed to do that very thing. If you want to get rid of Sample Value 1, then just use a script like this:

Const HKEY_CURRENT_USER = &H80000001

strComputer = “.”

Set objRegistry=GetObject(“winmgmts:\\” & _ strComputer & “\root\default:StdRegProv”)

strKeyPath = “Software\Test” strValueName = “Sample Value 1”

objRegistry.DeleteValue HKEY_CURRENT_USER, strKeyPath, strValueName

As we usually do when dealing with the registry, we begin by defining a constant (in this case HKEY_CURRENT_USER) that will tell our script which portion of the registry we want to work with. We then connect to WMI and the Standard Registry Provider; as usual, note that the StdRegProv class is found in the root\default namespace. Most of the WMI classes you work with for system administration are found in root\cimv2; that’s not the case with the Standard Registry Provider.

Next we define two variables. The first – strKeyPath – represents the path within the registry. We’re dealing with a key – Software\Test – that we made up ourselves; if you want to try this script as-is, you’ll need to create this key and the two sample values. Our second variable – strValueName – represents the name of the registry value that we want to delete.

Finally, we call the DeleteValue method, passing three parameters: the registry hive we’re working with (HKEY_CURRENT_USER); the registry path (strKeyPath); and the actual value to be deleted (strValueName). That’s all we have to do. Run the script and Sample Value 1 will be deleted, but everything else will be left alone.

And seeing as how someone’s bound to ask, what if you did want to delete the entire registry key? Again, no problem: just use the WMI DeleteKey method. Here’s a script that deletes the Software\Test registry key and everything in it:

Const HKEY_CURRENT_USER = &H80000001

strComputer = “.”

Set objRegistry=GetObject(“winmgmts:\\” & _ strComputer & “\root\default:StdRegProv”)

strKeyPath = “SOFTWARE\Test”

objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath

As you can see, this is even easier than deleting a specific value; all we have to do is let DeleteKey know the registry hive and the registry path, and it takes care of the rest. If only everything in scripting was that easy!

P.S. What’s that? You say you’d like to try this out, but you’re too … busy … to create the sample registry key and values? Okey-doke; here’s a script that’ll do that for you. (And you’re right: we do spoil you guys, don’t we?)

Const HKEY_CURRENT_USER = &H80000001

strComputer = “.”

Set objRegistry=GetObject(“winmgmts:\\” & _ strComputer & “\root\default:StdRegProv”)

strKeyPath = “Software\Test”

objRegistry.CreateKey HKEY_CURRENT_USER, strKeyPath

strValue = “” strValueName = “Sample Value 1” objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue

dwValue = 0 strValueName = “Sample Value 2” objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

0 comments

Discussion is closed.

Feedback usabilla icon