{"id":55283,"date":"2008-06-24T03:57:00","date_gmt":"2008-06-24T03:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/06\/24\/hey-scripting-guy-how-can-i-change-a-registry-value-named-default\/"},"modified":"2008-06-24T03:57:00","modified_gmt":"2008-06-24T03:57:00","slug":"hey-scripting-guy-how-can-i-change-a-registry-value-named-default","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-change-a-registry-value-named-default\/","title":{"rendered":"Hey, Scripting Guy! How Can I Change a Registry Value Named (Default)?"},"content":{"rendered":"<h2><img decoding=\"async\" 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\" \/> <\/h2>\n<p>Hey, Scripting Guy! I am trying to change a registry value that has the name (Default). I can change any registry value that has a \u201creal\u201d name, but I can\u2019t figure out how to change one with the name (Default). If I export the value the name shows up as @, but that doesn\u2019t work in my script, either. How do I change a registry value named (Default)?<\/p>\n<p>&#8212; TL<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" 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 decoding=\"async\" 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> <\/p>\n<p>Hey, TL. Well, it\u2019s Monday morning as this column is being written, and that comes as a surprise to at least a few people, most notably the Scripting Editor; after all, she didn\u2019t think the Scripting Guy who writes this column would even be <i>alive<\/i> come Monday morning, let alone be able to come in to work and write this column. That\u2019s because, for his weekend chore, the Scripting Guy who writes this column decided to buy a machete and attack the blackberry bushes that have been growing maniacally in the field behind his house.<\/p>\n<p>\u201cA machete?!?\u201d the Scripting Editor remarked when she heard of his plans. \u201cThose things are dangerous. Do you have any idea what you\u2019re doing?\u201d<\/p>\n<p>\u201cWhat difference does that make?\u201d replied the Scripting Guy who writes this column. \u201cWhen do I <i>ever<\/i> have any idea what I\u2019m doing?\u201d<\/p>\n<p>\u201cGood point,\u201d said the Scripting Editor.<\/p>\n<p>But all\u2019s well that ends well, right? The Scripting Guy who writes this column spent a good 4 or 5 hours hacking away at blackberry bushes on Sunday, and by the time he was done the blackberries had been defeated. (Well, temporarily; if you\u2019ve ever had to deal with blackberry bushes you know that they can never <i>truly<\/i> be defeated.) In addition to that, the Scripting Guy who writes this column managed to make it into work on time Monday morning. <\/p>\n<p>Oh, and just to put a little icing on the cake, he also had enough fingers and toes left to enable him to write a script that changes a registry value with the name (Default):<\/p>\n<pre class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001\n\nstrComputer = \".\"\n\nSet objRegistry = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\default:StdRegProv\")\n\nstrKeyPath = \"ScriptCenter\"\nstrValueName = \"\"\nstrValue = \"http:\/\/www.microsoft.com\/technet\/scriptcenter\/default.mspx\"\n\nobjRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue\n<\/pre>\n<p>There\u2019s not much to this script, and for good reason: as it turns out, it isn\u2019t very hard to modify the value of a (Default) key in the registry; you just need to know a little trick that lets you get at that value. <\/p>\n<p>So what <i>is<\/i> that trick? Be patient; we\u2019ll get to that in a second. First, however, we need to note that we\u2019re assuming that you have a registry key named ScriptCenter that lives in the HKEY_CURRENT_USER registry hive. That explains why we start our script off by defining a constant named HEY_CURRENT_USER and setting the value to &amp;H80000001: we\u2019ll need this constant to tell the script which registry hive we want to work with.<\/p>\n<p>After defining the constant our next step is to connect to the WMI service on the local computer, taking care to reference the <b>root\\default<\/b> namespace when binding to the <b>StdRegProv<\/b> class. Can we use this came script to modify a (Default) key on a remote computer? You already know the answer to that one: of course we can. All we have to do is assign the name of that remote computer to the variable strComputer:<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-ws-001\"\n<\/pre>\n<p>After we connect to the WMI service we then assign values to three different variables:<\/p>\n<pre class=\"codeSample\">strKeyPath = \"ScriptCenter\"\nstrValueName = \"\"\nstrValue = \"http:\/\/www.microsoft.com\/technet\/scriptcenter\/default.mspx\"\n<\/pre>\n<p>strKeyPath is simply the path to the registry key where the (Default) value lives. What if our registry key was something like Software\\Internal\\ScriptCenter? No problem; we simply assign that entire path to the variable strKeyPath:<\/p>\n<pre class=\"codeSample\">strKeyPath = \"Software\\Internal\\ScriptCenter\"\n<\/pre>\n<p>strValue is the value we want to assign to (Default); in this case, we\u2019re simply assigning (Default) the URL to the Script Center home page. <\/p>\n<p>That\u2019s cool, but what about this:<\/p>\n<pre class=\"codeSample\">strValueName = \"\"\n<\/pre>\n<p>As it turns out, this is the trick we were talking about. strValueName represents the name of the registry value we want to change. And you\u2019re right: we didn\u2019t actually <i>assign <\/i>strValueName a name, did we? And that\u2019s the trick: if you set strValueName to an empty string, then WMI will work against the (Default) registry value. <i>Why<\/i> does it work this way? Beats us. But it <i>does<\/i> work.<\/p>\n<table id=\"EOE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p><b>Note<\/b>. So do the Scripting Guys know stuff like this because they are highly-trained, highly-paid professionals who <i>have<\/i> to know stuff like this? No, not really. Instead, several years ago the Scripting Guy who writes this column was trying to write a script that would read information from the registry. Each time he ran the script, however, he got some very strange, very unexpected results. Eventually, he figured out that he had forgotten to assign the variable strValueName a value. Because of that, his script was reading the (Default) value instead. <i>That\u2019s<\/i> how he learned about this little trick.<\/p>\n<p>Needless to say, it\u2019s a good thing the Scripting Guys make as many mistakes as we do; otherwise, we wouldn\u2019t know much of anything!<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>At that point all we have to do is call the <b>SetStringValue<\/b> method and let SetStringValue do its thing:<\/p>\n<pre class=\"codeSample\">objRegistry.SetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue\n<\/pre>\n<p>Is that really going to work? If it doesn\u2019t, then the Scripting Guy who writes this column\u2019s name isn\u2019t the Scripting Guy who writes this column.<\/p>\n<table id=\"EHF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. Well, OK, technically his name <i>isn\u2019t<\/i> the Scripting Guy who writes this column. But you know what we mean.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>And sure, you can use this exact same approach to <i>read<\/i> the (Default) value in a registry key. Give this script a try and see for yourself:<\/p>\n<pre class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001\n\nstrComputer = \".\"\nSet objRegistry = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\default:StdRegProv\")\n\nstrKeyPath = \"ScriptCenter\"\nstrValueName = \"\"\n\nobjRegistry.GetStringValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue\n\nWscript.Echo strValue\n<\/pre>\n<p>That should do it, TL; like we said, this was actually an easy one, once you knew the trick. As it turns out, wielding a machete was equally easy (and surprisingly fun). Which, needless to say, came as a major disappointment to the Scripting Editor. After all, if anything ever happened to the Scripting Guy who writes this column, the Scripting Editor would finally be able to fulfill her lifelong dream: <i>she<\/i> would become the Scripting Guy who writes this column! Sorry, Scripting Editor; not this time.<\/p>\n<table id=\"EAG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>Ed<\/b><b>itor\u2019s Note<\/b>. The Scripting Editor can\u2019t imagine <i>anyone<\/i> in their right mind actually wanting to be the Scripting Guy who writes this column. Granted, the Scripting Editor isn\u2019t always in her right mind (she spends a limited amount of time in her left mind, and even more time out of her mind completely), but she\u2019s never been so far away from her right mind that she\u2019d want to be the Scripting Guy who writes this column.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>But don\u2019t give up: after all, the Scripting Guy who writes this column <i>still<\/i> has to climb up on the roof and take care of a few things up there. You might still get your chance after all. <\/p>\n<table id=\"EVG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p><b>Note<\/b>. The Scripting Guy who (still) writes this column was really only interested in cutting down blackberry bushes. However, it turns out that machetes are also useful against zombies and vampires. For example, according to the <a href=\"http:\/\/www.fvza.org\/machete.html\" target=\"_blank\"><b>Federal Vampire and Zombie Agency<\/b><\/a> \u201cA zombie without hands or fingers is much less of a threat, so aim your slashes for the zombie&#8217;s outstretched hands. Hacking at the hands will also help you avoid getting the blade stuck inside the zombie. As always, your objective should be to buy enough time to escape.\u201d<\/p>\n<p>Of course the Federal Vampire and Zombie Agency also says that \u201cThe long knob on the handle minimizes the chance of a machete slipping from the hand at the completion of a slashing blow.\u201d Obviously they haven\u2019t seen the Scripting Guy who writes this column in action, have they?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I am trying to change a registry value that has the name (Default). I can change any registry value that has a \u201creal\u201d name, but I can\u2019t figure out how to change one with the name (Default). If I export the value the name shows up as @, but that doesn\u2019t work [&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-55283","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! I am trying to change a registry value that has the name (Default). I can change any registry value that has a \u201creal\u201d name, but I can\u2019t figure out how to change one with the name (Default). If I export the value the name shows up as @, but that doesn\u2019t work [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55283","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=55283"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55283\/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=55283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}