{"id":70363,"date":"2005-02-24T08:52:00","date_gmt":"2005-02-24T08:52:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/24\/how-can-i-write-binary-data-to-the-registry\/"},"modified":"2005-02-24T08:52:00","modified_gmt":"2005-02-24T08:52:00","slug":"how-can-i-write-binary-data-to-the-registry","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-write-binary-data-to-the-registry\/","title":{"rendered":"How Can I Write Binary Data to the Registry?"},"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 write binary data to the registry?<BR><BR>&#8212; FG<\/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, FG. WMI\u2019s Standard Registry Provider includes a method &#8211; <B>SetBinaryData<\/B> &#8211; that makes it relatively easy to write binary data to the registry. There\u2019s one little catch to be aware of, but we\u2019ll let you know what that is.<\/P>\n<P>First, though, let\u2019s take a look at a script that writes the numbers 1 through 10 to a binary registry value named <B>BinaryTest<\/B> found in HKCU\\Software. If the referenced registry value exists then the script will simply replace the existing value with the new value (in this case, the numbers 1 though 10). If the value <I>doesn\u2019t<\/I> exist, the script will create BinaryTest and then assign it the numbers 1 through 10. In other words, you don\u2019t need to create BinaryTest before trying this sample script; if BinaryTest doesn\u2019t exist, WMI will create it for you.<\/P>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objRegistry = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;Software&#8221;\nstrValueName = &#8220;BinaryTest&#8221;\narrValues = Array(1,2,3,4,5,6,7,8,9,10)<\/p>\n<p>errReturn = objRegistry.SetBinaryValue _\n    (HKEY_CURRENT_USER, strKeyPath, strValueName, arrValues)\n<\/PRE>\n<P>The script begins by defining a constant named HKEY_CURRENT_USER and assigning it the value &amp;H80000001; this tells the Standard Registry Provider that we want to work with the HKEY_CURRENT_USER portion of the registry. We then connect to the WMI service; note that the Standard Registry Provider is found in the <B>root\\default<\/B> namespace. That\u2019s different from the vast majority of WMI classes used for system administration, most of which are found in root\\cimv2.<\/P>\n<P>We assign the key name <I>Software<\/I> to a variable named strKeyPath; this is the registry path within HKCU. A second variable &#8211; strValueName &#8211; is assigned the value <I>BinaryTest<\/I>, which happens to be the registry value we want to change. Finally we use this line of code to assign the numbers 1 through 10 to an array variable we named arrValues; this, of course, represents the binary data we want to write to the registry:<\/P><PRE class=\"codeSample\">arrValues = Array(1,2,3,4,5,6,7,8,9,10)\n<\/PRE>\n<P>Having defined our constant and assigned values to all our variables, all that\u2019s left is to call the <B>SetBinaryValue<\/B> metod to write these 10 values to the registry:<\/P><PRE class=\"codeSample\">errReturn = objRegistry.SetBinaryValue _\n    (HKEY_CURRENT_USER, strKeyPath, strValueName, arrValues)\n<\/PRE>\n<P>So what\u2019s the catch? Well, if you\u2019re familiar with binary values in the registry, you know that numbers are represented by hexadecimal values. You don\u2019t see the number <B>1<\/B> in a binary registry value; you see <B>01<\/B>. Likewise you don\u2019t see the number <B>10<\/B> in a binary registry value; you see <B>0A<\/B>. The registry works with hexadecimal values, but the SetBinaryValue method requires us to pass regular decimal numbers. That can be a little bit confusing if you\u2019re following instructions for setting a registry value and the instructions say, \u201cSet the value to 0A.\u201d Before you can use the SetBinaryValue method you\u2019ll need to convert 0A to its decimal equivalent. (An easy way to do that? Use Calculator in Scientific mode.) <\/P>\n<P>Other than that, though, setting a binary value in the registry is no different &#8211; and no more difficult &#8211; than setting a string value or a numeric value.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I write binary data to the registry?&#8212; FG Hey, FG. WMI\u2019s Standard Registry Provider includes a method &#8211; SetBinaryData &#8211; that makes it relatively easy to write binary data to the registry. There\u2019s one little catch to be aware of, but we\u2019ll let you know what that is. First, though, [&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-70363","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 write binary data to the registry?&#8212; FG Hey, FG. WMI\u2019s Standard Registry Provider includes a method &#8211; SetBinaryData &#8211; that makes it relatively easy to write binary data to the registry. There\u2019s one little catch to be aware of, but we\u2019ll let you know what that is. First, though, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70363","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=70363"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70363\/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=70363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}