{"id":70303,"date":"2005-03-04T08:43:00","date_gmt":"2005-03-04T08:43:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/04\/how-can-i-delete-a-single-registry-value\/"},"modified":"2005-03-04T08:43:00","modified_gmt":"2005-03-04T08:43:00","slug":"how-can-i-delete-a-single-registry-value","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-a-single-registry-value\/","title":{"rendered":"How Can I Delete a Single Registry Value?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I delete a single registry value? I don\u2019t want to delete the entire registry key, just one value.<BR><BR>&#8212; BF<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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\u2019s make sure everyone is clear what we\u2019re talking about. As you can see in the following figure (well, assuming you put this Web page under a microscope; <A href=\"http:\/\/null\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/registry-big.jpg\" target=\"_blank\"><B>click here<\/B><\/A> if you\u2019d like to see a full-size image), we have a registry <I>key<\/I> named Test in the left pane and two registry <I>values<\/I> &#8211; Sample Value 1 and Sample Value 2 &#8211; in the right pane:<\/P><IMG border=\"0\" alt=\"The Windows Registry\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/registry-small.jpg\" width=\"335\" height=\"171\"> \n<P>You\u2019d like to delete one of these values (say, Sample Value 1) without deleting the other value and without deleting the entire key.<\/P>\n<P>No problem: WMI has a <B>DeleteValue<\/B> method designed to do that very thing. If you want to get rid of Sample Value 1, then just use a script like this:<\/P><PRE class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objRegistry=GetObject(&#8220;winmgmts:\\\\&#8221; &amp; _ \n    strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;Software\\Test&#8221;\nstrValueName = &#8220;Sample Value 1&#8221;<\/p>\n<p>objRegistry.DeleteValue HKEY_CURRENT_USER, strKeyPath, strValueName\n<\/PRE>\n<P>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 <B>root\\default<\/B> namespace. Most of the WMI classes you work with for system administration are found in root\\cimv2; that\u2019s not the case with the Standard Registry Provider.<\/P>\n<P>Next we define two variables. The first &#8211; strKeyPath &#8211; represents the path within the registry. We\u2019re dealing with a key &#8211; Software\\Test &#8211; that we made up ourselves; if you want to try this script as-is, you\u2019ll need to create this key and the two sample values. Our second variable &#8211; strValueName &#8211; represents the name of the registry value that we want to delete.<\/P>\n<P>Finally, we call the DeleteValue method, passing three parameters: the registry hive we\u2019re working with (HKEY_CURRENT_USER); the registry path (strKeyPath); and the actual value to be deleted (strValueName). That\u2019s all we have to do. Run the script and Sample Value 1 will be deleted, but everything else will be left alone.<\/P>\n<P>And seeing as how someone\u2019s bound to ask, what if you <I>did<\/I> want to delete the entire registry key? Again, no problem: just use the WMI <B>DeleteKey<\/B> method. Here\u2019s a script that deletes the Software\\Test registry key and everything in it:<\/P><PRE class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objRegistry=GetObject(&#8220;winmgmts:\\\\&#8221; &amp; _ \n    strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;SOFTWARE\\Test&#8221;<\/p>\n<p>objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath\n<\/PRE>\n<P>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>\n<P>P.S. What\u2019s that? You say you\u2019d <I>like<\/I> to try this out, but you\u2019re too \u2026 busy \u2026 to create the sample registry key and values? Okey-doke; here\u2019s a script that\u2019ll do that for you. (And you\u2019re right: we <I>do<\/I> spoil you guys, don\u2019t we?)<\/P><PRE class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objRegistry=GetObject(&#8220;winmgmts:\\\\&#8221; &amp; _ \n    strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;Software\\Test&#8221;<\/p>\n<p>objRegistry.CreateKey HKEY_CURRENT_USER, strKeyPath<\/p>\n<p>strValue = &#8220;&#8221;\nstrValueName = &#8220;Sample Value 1&#8221;\nobjRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue<\/p>\n<p>dwValue = 0\nstrValueName = &#8220;Sample Value 2&#8221;\nobjRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue\n<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I delete a single registry value? I don\u2019t want to delete the entire registry key, just one value.&#8212; BF 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,26,3,5],"class_list":["post-70303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-registry","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I delete a single registry value? I don\u2019t want to delete the entire registry key, just one value.&#8212; BF 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70303"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70303\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}