{"id":66023,"date":"2006-11-16T18:07:00","date_gmt":"2006-11-16T18:07:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/11\/16\/how-can-i-create-a-new-registry-key\/"},"modified":"2006-11-16T18:07:00","modified_gmt":"2006-11-16T18:07:00","slug":"how-can-i-create-a-new-registry-key","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-new-registry-key\/","title":{"rendered":"How Can I Create a New Registry Key?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><\/p>\n<p>Hey, Scripting Guy! How can I create a new registry key?<\/p>\n<p>&#8212; DL<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/a><\/p>\n<p>Hey, DL. First of all, for those of you who get all your news from the <i>Hey, Scripting Guy!<\/i> column (as if there was any other source for news and information?) we are happy to report that the University of Washington men\u2019s basketball team defeated a very good Northern Iowa squad 70-61 to win the Basketball Travelers Classic. Admittedly, it wasn\u2019t always pretty: the Huskies are an incredibly young team, occasionally putting 5 freshmen on the floor at the same time. And the officiating was \u2026 interesting \u2026 to say the least: the refs allowed the players to shake hands before the game started, but that was pretty much the only time any contact was allowed. (We think a couple fouls were even called because a player \u201cviolated someone\u2019s personal space.\u201d) But it was fun, and while they are apt to experience growing pains along the way, the Huskies should be a pretty good team come NCAA tournament time.<\/p>\n<p>What\u2019s that? <i>Other<\/i> news and information? No, as far as we know the Washington-Northern Iowa game was pretty much the only thing of interest going on in the world.<\/p>\n<table class=\"dataTable\" id=\"EED\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Trivia note<\/b>. In case you\u2019re wondering, it\u2019s the Northern Iowa Panthers. Of course, the Scripting Guy who writes this column already knew that, just as he knew that it\u2019s the Pepperdine Waves and the Nicholls State Colonels. Depending on how you want to look at it, that\u2019s either very impressive (Delaware Fighting Blue Hens, anyone?) or kind of sad.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Wait, hold everything: we\u2019ve just been handed an important news flash! Sources tell us that <i>this<\/i> is how you create a new registry key:<\/p>\n<pre class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001\nstrComputer = \".\"\nSet objRegistry = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\default:StdRegProv\")\nstrKeyPath = \"SOFTWARE\\Script Center\"\nobjRegistry.CreateKey HKEY_CURRENT_USER, strKeyPath\n<\/pre>\n<p>According to a spokesperson for the Scripting Guys \u2013 who asked that his name not be used \u2013 the script starts out by defining a constant named HKEY_CURRENT_USER and setting the value to &amp;H80000001; this results in the new registry key being created in the HKEY_CURRENT_USER portion of the registry. What if you wanted to create the new registry key in HKEY_LOCAL_MACHINE? No problem; just change the first line of code to look like this, and replace all instances of HKEY_CURRENT_USER in the script with HKEY_LOCAL_MACHINE:<\/p>\n<pre class=\"codeSample\">Const HKEY_LOCAL_MACHINE = &amp;H80000002\n<\/pre>\n<p>After defining the constant we next connect to the WMI service on the local computer. And sure, you can create a registry key on a remote machine; just assign the name of that remote computer to the variable strComputer. Something like this:<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-fs-01\"\n<\/pre>\n<p>One thing to watch out for: make sure you specify <b>\\root\\default<\/b> when connecting to the <b>StdRegProv<\/b> class. In other words, don\u2019t use \\root\\cimv2, like you do for most WMI scripts. Why not? That\u2019s easy: the StdRegProv class doesn\u2019t live in \\root\\cimv2; it\u2019s housed in \\root\\default instead.<\/p>\n<p>Believe it or not, at this point we\u2019re almost done. Our next chore is to assign the registry path for the new registry key. We want to create a key named SOFTWARE\\Script Center in HKEY_CURRENT_USER; therefore, we assign SOFTWARE\\Script Center to a variable named strKeyPath:<\/p>\n<pre class=\"codeSample\">strKeyPath = \"SOFTWARE\\Script Center\"\n<\/pre>\n<p>That\u2019s a good point. It\u2019s not surprising that we can create a key named SOFTWARE\\Script Center; after all, the SOFTWARE key already exists. But what if we wanted to create a key named <b>SOFTWARE\\Script Center\\My Registry Keys\\Test Key<\/b>? Do we need to create a Script Center key, then create a My Registry Keys key, and then, finally, create a Test Key key? <\/p>\n<p>Fortunately, the answer is no. Instead, just assign the entire path to the variable strKeyPath:<\/p>\n<pre class=\"codeSample\">strKeyPath = \"SOFTWARE\\Script Center\\My Registry Keys\\Test Key\"\n<\/pre>\n<p>When you run the script WMI will automatically create any and all the required keys and subkeys.<\/p>\n<p>Speaking of which, to actually create the new registry key we simply call the <b>CreateKey<\/b> method, passing as parameters the registry hive (HKEY_CURRENT_USER) and the path to the new key (strKeyPath):<\/p>\n<pre class=\"codeSample\">objRegistry.CreateKey HKEY_CURRENT_USER, strKeyPath\n<\/pre>\n<p>Just like that, we\u2019ve got ourselves a brand-new registry key.<\/p>\n<p>You know, we\u2019ve got a few minutes here, so why don\u2019t we take our new registry key out for a test spin; after all, there\u2019s no point in having a registry key if you don\u2019t get to do fun things like create new registry values in that key. Here\u2019s a sample script that adds a new DWORD value \u2013 B \u2013 to our new registry key, assigning My DWORD Value the value 13:<\/p>\n<pre class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001\nstrComputer = \".\"\nSet objRegistry = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\default:StdRegProv\")\nstrKeyPath = \"SOFTWARE\\Script Center\"\nstrValueName = \"My DWORD Value\"\ndwValue = 13\nobjRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue\n<\/pre>\n<p>We won\u2019t explain this script in any detail today; for more information on working with the registry, see the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_reg_overview.mspx\"><b>Microsoft Windows 2000 Scripting Guide<\/b><\/a>. For now, we\u2019ll just note that we can create a new registry value simply by specifying the path to that value and then calling \u2013 in this case \u2013 the <b>SetDWORDValue<\/b> method. If the registry value in question already exists the script will modify that value; in this example, that means setting the value to 13. If the registry value <i>doesn\u2019t<\/i> exist then the script will automatically create the new value and then set it to 13. Could there be anything easier? Maybe, but we haven\u2019t found it yet. (And trust us, we Scripting Guys are always on the lookout for easy things.)<\/p>\n<p>Don\u2019t want to create a DWORD value? No problem; just use one of these methods to create a different type of registry value:<\/p>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>SetStringValue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>SetExpandedStringValue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>SetMultiStringValue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p>SetBinaryValue<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Hope that helps, DL. You say you have another question? Doesn\u2019t the University of Washington also have a football team, one that \u2013 five weeks into the season \u2013 looked like they were finally going to turn things around and return the Huskies to the college football prominence they enjoyed for so long? Sadly, the answer is no: as near as we can tell, the UW no longer has a football team. Thank goodness for basketball, huh?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a new registry key? &#8212; DL Hey, DL. First of all, for those of you who get all your news from the Hey, Scripting Guy! column (as if there was any other source for news and information?) we are happy to report that the University of Washington men\u2019s [&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-66023","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 create a new registry key? &#8212; DL Hey, DL. First of all, for those of you who get all your news from the Hey, Scripting Guy! column (as if there was any other source for news and information?) we are happy to report that the University of Washington men\u2019s [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66023","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=66023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66023\/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=66023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}