{"id":63453,"date":"2007-12-06T01:04:00","date_gmt":"2007-12-06T01:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/12\/06\/how-can-i-start-a-script-in-a-hidden-window\/"},"modified":"2007-12-06T01:04:00","modified_gmt":"2007-12-06T01:04:00","slug":"how-can-i-start-a-script-in-a-hidden-window","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-start-a-script-in-a-hidden-window\/","title":{"rendered":"How Can I Start a Script in a Hidden Window?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! How can I start a script in a hidden window?<BR><BR>&#8212; OT<\/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, OT. You know, today is kind of a disappointing day for the Scripting Guys, or at least for the Scripting Guy who writes this column. As we noted <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/dec07\/hey1204.mspx\"><B>yesterday<\/B><\/A>, the Seattle area was hit by a freak \u201c<A href=\"http:\/\/en.wikipedia.org\/wiki\/Pineapple_Express\" target=\"_blank\"><B>Pineapple Express<\/B><\/A>\u201d rainstorm that seemed destined to set all sorts of records for most precipitation to fall on the region in a single day. Alas, we ended up well short of the record: officially we recorded 3.77 inches of rain in 24 hours, nowhere near the record of 5 inches plus. To be honest, the Scripting Guy who writes this column would have preferred that we got no rain at all; nevertheless, if we <I>had<\/I> to get a bunch of rain, well, then we might as well have gone for it and set a record. Instead, we\u2019re reduced to this:<\/P>\n<P>\u201cHey, Scripting Guy who writes that column: I heard you guys got a lot of rain the other day.\u201d<\/P>\n<P>\u201cWe sure did: 3.77 inches.\u201d<\/P>\n<P>\u201cWow, that must be a record!\u201d<\/P>\n<P>\u201cWell, actually, no; I\u2019m afraid it wasn\u2019t even close to the record.\u201d<\/P>\n<P>\u201cOh \u2026. You know, I think I\u2019ll go talk to that guy from Bremerton. Did you hear that they got 10.78 inches that same day? Now <I>that\u2019s<\/I> rain!\u201d<\/P>\n<P>Such is life in Seattle; seems like around here the best we can <I>ever<\/I> do is \u201calmost.\u201d For example, we <I>almost<\/I> have a plan to solve all traffic problems: in the year 2010, 14 years (and several billion dollars) after it was voted in, we\u2019ll have a light rail system that runs from downtown Seattle to the airport. (Well, <I>almost<\/I> to the airport.) In Seattle, we <I>almost<\/I> have a professional basketball team; barring a miracle of some sort, the Sonics are bound for Oklahoma City. (We\u2019re not sure what the people of Oklahoma City did to deserve that, but we don\u2019t want to say anything lest they change their mind.) And, of course, we <I>almost<\/I> set a record for the most rainfall in a single day. <\/P>\n<P>So near and yet so far.<\/P>\n<TABLE id=\"EGE\" 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 to readers from Bremerton<\/B>. We know you\u2019re proud of your 10.78 inches of rain, as well you should be; you whipped us fair and square. But don\u2019t start feeling <I>too<\/I> smug; after all, the city of Holt, MO once received 12 inches of rain in just 42<I> minutes<\/I>. Give us a call when you beat that record; <I>then<\/I> we\u2019ll be impressed.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Anyway, now that things are beginning to dry out (well, <I>almost<\/I> beginning to dry out) we can concentrate on other things, things like, say, starting a script in a hidden window. <\/P>\n<P>So how <I>do<\/I> you start a script in a hidden window? Well, there are actually a couple of ways to do this. For example, you could simply write a script like the following:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run &#8220;Wscript.exe C:\\Scripts\\Test.vbs&#8221;\n<\/PRE>\n<P>This <I>usually<\/I> works simply because the Wscript script host doesn\u2019t use the command window; because of that, if you start a script under Wscript you won\u2019t see anything happen on screen. Well, unless the script echoes back information, that is; in that case, you\u2019ll see a message box each time the Wscript.Echo command gets called. However, as long as you stay clear of anything that would cause a message box or input box to appear on screen you can simply run a script under the Wscript script host and rest assured that no one will see that script running.<\/P>\n<P>So, if that\u2019s all true (and it is), why didn\u2019t we just give you the preceding script and call it good? Well, we could have, except that the Scripting Guys are always on the lookout for scripts that can be used in more than one scenario. The preceding script works great for .VBS scripts, but it also works <I>only<\/I> for .VBS scripts. (Which makes sense: you can\u2019t run, say, a command-line tool under one of the Windows Script Host script hosts.)<\/P>\n<P>Therefore, we decided to go with a different approach, an approach that can run pretty much <I>anything<\/I> in a hidden window. An approach, come to think of it, that looks remarkably like the following:<\/P><PRE class=\"codeSample\">Const HIDDEN_WINDOW = 0<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set objStartup = objWMIService.Get(&#8220;Win32_ProcessStartup&#8221;)\nSet objConfig = objStartup.SpawnInstance_\nobjConfig.ShowWindow = HIDDEN_WINDOW<\/p>\n<p>Set objProcess = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2:Win32_Process&#8221;)<\/p>\n<p>objProcess.Create &#8220;Cscript.exe C:\\Scripts\\Test.vbs&#8221;, null, objConfig, intProcessID\n<\/PRE>\n<P>So what\u2019s going on <I>here<\/I>? Good question. As you can see, we start out by defining a constant named HIDDEN_WINDOW and setting the value to 0; as you might expect, we\u2019ll use this constant to tell the script that we want our second script (or our command-line tool) to start up in a hidden window. After defining the constant we then connect to the WMI service on the local computer. <\/P>\n<P>What\u2019s that? Could we run this same script on a <I>remote<\/I> computer? Yes, we could, although it would be overkill. Why? Because we don\u2019t need to configure all the startup options (which we\u2019re about to do) if we\u2019re going to run the script on a remote computer; after all, any processes started on a remote machine <I>always<\/I> run in a hidden window. But, sure, you can leave this script as-is and run it on a remote machine; just assign the name of that remote machine to the variable strComputer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-001&#8221;\n<\/PRE>\n<P>After we connect to the WMI service we then run smack-dab into <I>this<\/I> block of code:<\/P><PRE class=\"codeSample\">Set objStartup = objWMIService.Get(&#8220;Win32_ProcessStartup&#8221;)\nSet objConfig = objStartup.SpawnInstance_\nobjConfig.ShowWindow = HIDDEN_WINDOW\n<\/PRE>\n<P>As you might have guessed, this is the portion of the script where we configure our startup options. To do that, we first create an object reference to the <B>Win32_ProcessStartup<\/B> class; this is the class that \u2013 well, yeah, this is the class that enables you to configure startup options for a process. After we connect to the Win32_ProcessStartup class we then call the <B>SpawnInstance_<\/B> method (note the underscore at the end of the method name); this gives us a \u201cblank\u201d startup object we can then configure to our heart\u2019s content. For this particular script our heart is content merely to set the value of the <B>ShowWindow<\/B> property to 0 (using the constant HIDDEN_WINDOW):<\/P><PRE class=\"codeSample\">objConfig.ShowWindow = HIDDEN_WINDOW\n<\/PRE>\n<P>What will that do? You got it: that will cause the script (or command-line tool) to start up in a hidden window.<\/P>\n<P>By the way, there are lots of other things you can do with the Win32_ProcessStartup class; for example, you can start a process in a minimized window or a maximized window, or you can start a process with a higher (or lower) priority than normal. For more information, see the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa394375.aspx\" target=\"_blank\"><B>WMI SDK<\/B><\/A> on MSDN. Just to whet your appetite, here\u2019s code that configures any script or application that runs in a command window. In this case, that command window will:<\/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>Have the Window title <I>Test Window<\/I>.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Have a bright red background.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Be located squarely in the upper left-hand corner of the screen.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Here\u2019s the code:<\/P><PRE class=\"codeSample\">Set objStartup = objWMIService.Get(&#8220;Win32_ProcessStartup&#8221;)\nSet objConfig = objStartup.SpawnInstance_\nobjConfig.Title = &#8220;Test Window&#8221;\nobjConfig.FillAttribute = 192 \nobjConfig.X = 0\nobjConfig.Y = 0\n<\/PRE>\n<P>Again, see the WMI SDK for more details.<\/P>\n<P>As for <I>our<\/I> script, after we configure the startup options we use the following line of code to bind to the <B>Win32_Process<\/B> class:<\/P><PRE class=\"codeSample\">Set objProcess = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2:Win32_Process&#8221;)\n<\/PRE>\n<P>From there we call the Create method to actually run our script (or command-line tool) in a hidden window:<\/P><PRE class=\"codeSample\">objProcess.Create &#8220;Cscript.exe C:\\Scripts\\Test.vbs&#8221;, null, objConfig, intProcessID\n<\/PRE>\n<P>As you can see, we need to pass Create 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>Cscript.exe C:\\Scripts\\Test.vbs<\/B>. Full path to the .VBS script we want to run. By prefacing the path with Cscript.exe that causes the script to run under the CScript script host. That\u2019s a good thing to do here, because then any Wscript.Echo commands will simply be echoed to the hidden console window. If we run Test.vbs under the Wscript script host then any such commands will be echoed back to the screen in the form of message boxes. That\u2019s fine, except for one thing: those message boxes will pop up in a hidden window, and we won\u2019t be able to see them. In turn, that means those message boxes will sit there forever waiting (in vain) for someone to click them. And until they do get clicked, the script will also sit there waiting, forever and ever.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Null<\/B>. Specifies the working directory for the script or command-line tool. If set to Null (like here) that simply means that we want Test.vbs to run in the same directory as the script that started it.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>objConfig<\/B>. Object reference to our startup options object.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>intProcessID<\/B>. An \u201cout parameter\u201d that holds the PID (process ID) assigned to our new process. All we do is supply a variable name; the Create method will then populate this variable for us.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That\u2019s all we have to do: start this script and Test.vbs will run, but it will run in a hidden window and no one will ever be the wiser. See you all tomorrow!<\/P>\n<P>Wait, hold on a second; we wouldn\u2019t feel right about leaving without giving you a few more rainfall statistics. What kind of statistics? How about this one: on July 4, 1956 the city of Unionville, MD got 1.23 inches of rain in a single <I>minute<\/I>. (Wonder how many people got caught in that after saying, \u201cI\u2019ll be right back; I just need to dash outside for a minute.\u201d) In 1979 the city of Alvin, TX received 42 inches of rain in a single 24-hour period. And, believe it or not, that\u2019s nothing: in 1966 <I>72<\/I> inches of rain fell on the tiny island of La R\u00e9union in a single day. What a great day that must have been, huh?<\/P>\n<TABLE id=\"EZAAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. If you\u2019re thinking, \u201cLa R\u00e9union? That sounds like a neat place to for a vacation,\u201d well, consider this: in 1980, La R\u00e9union received 46 inches of rain in a <I>12-hour<\/I> period. That was part of a 5-day span in which 156 inches of rain (over 31 inches per day) fell on the island.<\/P>\n<P>But, yeah, it <I>does<\/I> sound like a neat place for a vacation, doesn\u2019t it?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Last, but surely not least, we have Cherrapunji, India. In 1860, the same year the Scripting Editor graduated from high school, Cherrapunji received a grand total of 1,042 inches (nearly 87 <I>feet<\/I>) of rain. That means, on average, Cherrapunji received almost 3 inches of rain every single day for an entire year. Good luck beating <I>that<\/I> record, Bremerton!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I start a script in a hidden window?&#8212; OT Hey, OT. You know, today is kind of a disappointing day for the Scripting Guys, or at least for the Scripting Guy who writes this column. As we noted yesterday, the Seattle area was hit by a freak \u201cPineapple Express\u201d rainstorm [&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":[2,3,4,5],"class_list":["post-63453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I start a script in a hidden window?&#8212; OT Hey, OT. You know, today is kind of a disappointing day for the Scripting Guys, or at least for the Scripting Guy who writes this column. As we noted yesterday, the Seattle area was hit by a freak \u201cPineapple Express\u201d rainstorm [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63453","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=63453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63453\/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=63453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}