{"id":67673,"date":"2006-03-27T16:59:00","date_gmt":"2006-03-27T16:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/27\/how-can-i-determine-the-default-script-host-on-a-computer-before-i-run-a-script\/"},"modified":"2006-03-27T16:59:00","modified_gmt":"2006-03-27T16:59:00","slug":"how-can-i-determine-the-default-script-host-on-a-computer-before-i-run-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-default-script-host-on-a-computer-before-i-run-a-script\/","title":{"rendered":"How Can I Determine the Default Script Host on a Computer Before I Run a Script?"},"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! I know that when I run a script I can use code to determine whether the script is running under WScript or CScript. What I can\u2019t figure out is this: how can I determine the default script host on a computer <I>before<\/I> I run a script?<BR><BR>&#8212; AT<\/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, AT. You know, it\u2019s not very often that people completely stump the Scripting Guys; however, we have to admit this question posed a bit of a challenge. (OK, OK: so maybe it\u2019s not <I>that<\/I> hard to stump the Scripting Guys; to be honest, it\u2019s actually pretty easy. But you guys don\u2019t need to know that.) We had no idea how to answer this question, and we knew it was going to take a lot of hard work and dedication in order to help you out with this.<\/P>\n<P>But, in the face of adversity, did the Scripting Guys give up? Well, as a matter of fact we did. However, in the process of doing something totally unrelated, we accidentally stumbled upon the answer. Here, using a somewhat-roundabout methodology, is a script that will tell you the default script host on a computer:<\/P><PRE class=\"codeSample\">Const HKEY_CLASSES_ROOT = &amp;H80000000<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objRegistry = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;VBSFile\\Shell\\Open\\Command&#8221;\nobjRegistry.GetExpandedStringValue HKEY_CLASSES_ROOT,strKeyPath,vbNullString,strValue<\/p>\n<p>strValue = LCase(strValue)<\/p>\n<p>If InStr(strValue, &#8220;wscript.exe&#8221;) then\n    Wscript.Echo &#8220;WScript&#8221;\nElse\n    Wscript.Echo &#8220;CScript&#8221;\nEnd If\n<\/PRE>\n<P>As it turns out, information about the default script host is stored in the registry, although not in a particularly obvious location. What you need to do is open HKEY_CLASSES_ROOT, find the registry key VBSFile\\Shell\\Open\\Command and then look at the default value. (The default value is an otherwise-unnamed registry value, one that shows up as <I>(Default)<\/I> in Regedit.) The value of (Default) will be the path to the script host executable (e.g., %SystemRoot%\\System32\\WScript.exe). In turn, the executable file tells you whether the default script host is CScript or WScript.<\/P>\n<P>So how do we actually get to that value? Well, we begin by defining a constant named HKEY_CLASSES_ROOT and setting the value to &amp;H80000000; that tells the script which registry hive we want to work with. We then connect to the WMI service on the local computer (although we could just as easily run this script against a remote machine), taking care to bind to the <B>root\\default<\/B> namespace. (Most WMI scripts use the root\\cimv2 namespace, but, for some reason, the System Registry Provider lives in root\\default instead. Probably cheaper housing or better schools.)<\/P>\n<P>After making the connection we create a variable named strKeyPath, assigning it the value VBSFile\\Shell\\Open\\Command. At that point we\u2019re ready to read the value from the registry.<\/P>\n<P>What\u2019s that? Didn\u2019t we forget something? No, we didn\u2019t forget anything. (Although now that you mention it we\u2019re beginning to wonder if anyone turned off the stove when we left the house this morning.) You\u2019re right that, in most WMI registry scripts, we would need to assign values to <I>two<\/I> variables, one to represent the registry key, the other to represent the desired value within that registry key. In this case, however, we don\u2019t need to create a variable in which to store the name of the registry value. Why not? Because, technically, (Default) values don\u2019t actually <I>have<\/I> a name; they\u2019re just, well, the default values. What we\u2019ll end up doing is telling the script to read a Null value from this particular registry key. That doesn\u2019t sound like a very good plan but, fortunately, the Registry Provider will know that this means to read the (Default) value. <\/P>\n<P>In fact, as long as we\u2019re on the subject, here\u2019s the line of code that retrieves the value for us:<\/P><PRE class=\"codeSample\">objRegistry.GetExpandedStringValue HKEY_CLASSES_ROOT,strKeyPath,vbNullString,strValue\n<\/PRE>\n<P>Because (Default) happens to have the REG_EXPAND_SZ data type we call the <B>GetExpandedStringValue<\/B> method, passing four parameters:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>HKEY_CLASSES_ROOT<\/B>, the constant that tells the script which registry hive to work with.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strKeyPath<\/B>, the variable that tells the script which registry key to work with.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>vbNullString<\/B>, a VBScript constant representing a Null value. This is where we would usually insert a variable representing the value name but, as we noted, this time around our value doesn\u2019t <I>have<\/I> a name.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strValue<\/B>, an \u201cout\u201d parameter. GetExpandedStringValue needs a place to store the value it reads from the registry; an out parameter is simply a placeholder variable where that value can be stashed.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>As soon as GetExpandedStringValue does its thing we use this line of code to change all the characters in strValue to lowercase; we do that to make it easier to determine whether the value contains a particular string or not:<\/P><PRE class=\"codeSample\">strValue = LCase(strValue)\n<\/PRE>\n<P>Finally, we call the <B>InStr<\/B> function, looking to see if the string value <I>wscript.exe<\/I> can be found anywhere within strValue. If it can, then we echo back the fact that the default script host is WScript; if it can\u2019t, then that must mean the default script host is CScript:<\/P><PRE class=\"codeSample\">If InStr(strValue, &#8220;wscript.exe&#8221;) then\n    Wscript.Echo &#8220;WScript&#8221;\nElse\n    Wscript.Echo &#8220;CScript&#8221;\nEnd If\n<\/PRE>\n<P>So there you go, AT: now you can determine the default script host on a computer without running a script. (Yes, we know, you have to run the script that determines the default script host\u2026but you get the point.) We hope you find this useful, and we hope everyone has learned an important lesson today: try as you might, you can\u2019t stump the Scripting Guys.<\/P>\n<P>Well, not unless you ask us a question we can\u2019t answer. But you\u2019ll never stump us by asking a question that we <I>can<\/I> answer.<\/P>\n<P>OK: <I>almost<\/I> never.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I know that when I run a script I can use code to determine whether the script is running under WScript or CScript. What I can\u2019t figure out is this: how can I determine the default script host on a computer before I run a script?&#8212; AT Hey, AT. You know, it\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":[16,31,26,2,3,4,5],"class_list":["post-67673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-operating-system","tag-registry","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I know that when I run a script I can use code to determine whether the script is running under WScript or CScript. What I can\u2019t figure out is this: how can I determine the default script host on a computer before I run a script?&#8212; AT Hey, AT. You know, it\u2019s [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67673","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=67673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67673\/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=67673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}