{"id":67283,"date":"2006-05-19T18:04:00","date_gmt":"2006-05-19T18:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/05\/19\/how-can-i-hide-the-command-window-when-executing-a-command-like-net-localgroup-administrators\/"},"modified":"2006-05-19T18:04:00","modified_gmt":"2006-05-19T18:04:00","slug":"how-can-i-hide-the-command-window-when-executing-a-command-like-net-localgroup-administrators","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-hide-the-command-window-when-executing-a-command-like-net-localgroup-administrators\/","title":{"rendered":"How Can I Hide the Command Window When Executing a Command Like net Localgroup Administrators?"},"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 hide the command window when executing a command like <B>net localgroup Administrators<\/B>?<BR><BR>&#8212; AA<\/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, AA. You know, before we begin we\u2019d like to take a few minutes to recognize all those faithful readers who read the <I>Hey, Scripting Guy!<\/I> column each and every day. Hey, June; how\u2019s it going?<\/P>\n<P>No, that\u2019s pretty much it. Maybe we should have said we wanted to recognize our faithful <I>reader<\/I>. <\/P>\n<P>Hmmm, that\u2019s an interesting idea: if we actually answered the questions instead of rambling on and on maybe we <I>would<\/I> have more than just one faithful reader. What the heck; we\u2019re willing to try anything:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nstrCommand = &#8220;net localgroup Administrators&#8221; <\/p>\n<p>Set objExec = objShell.Exec(strCommand) <\/p>\n<p>Do Until objExec.Status\n    Wscript.Sleep 250\nLoop <\/p>\n<p>Wscript.Echo objExec.StdOut.ReadAll()\n<\/PRE>\n<P>To begin with, AA, we\u2019re assuming that you\u2019ve been using the Windows Script Host <B>Run<\/B> method in order to run net localgroup Administrators. And, yes, that will cause a command window to open up on screen. (Even worse, if you don\u2019t include the proper parameters that window will open and then close before you ever have a chance to read the information that net localgroup Administrators returns.) What you want to do is run your command-line utility without a second command window opening up and then have the information returned from net localgroup Administrators displayed in the original command window. In other words, you\u2019d like your entire session to look something like this:<\/P><PRE class=\"codeSample\">C:\\scripts&gt;cscript hidden.vbs\nMicrosoft (R) Windows Script Host Version 5.6\nCopyright (C) Microsoft Corporation 1996-2001. All rights reserved.<\/p>\n<p>Alias name     Administrators\nComment        Administrators have complete and unrestricted access to the computer\/domain<\/p>\n<p>Members<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-\nAdministrator\nKen Myer\nkenmyer\nFABRIKAM\\InfoSec Secure Environment\nFABRIKAM\\Domain Admins\nFABRIKAM\\kenmyer<\/p>\n<p>The command completed successfully.\n<\/PRE>\n<P>Can we do that? You bet we can, as long as we use the <B>Exec<\/B> method rather than the Run method. When you run a command-line tool from Exec the script will not open up a second command window. Instead, the script runs in an invisible window, and the data returned by the tool gets stored in the <B>StdOut<\/B> property. All we have to do is read in the value of StdOut and we\u2019ll have complete access to the returned data.<\/P>\n<P>Which is exactly what we do in our script. We start out by creating an instance of the <B>Wscript.Shell<\/B> object, then assign the name of our command-line command (net localgroup Administrators) to a variable named strCommand. After doing that tiny bit of setup we can run the command using this single line of code:<\/P><PRE class=\"codeSample\">Set objExec = objShell.Exec(strCommand)\n<\/PRE>\n<P>As you can see, we call the Exec method, passing it the variable strCommand. At the same time we create an object reference named objExec. That\u2019s required because the Exec method actually creates an instance of an object: the WshScriptExec object.<\/P>\n<P>No, that\u2019s not a bad thing; that\u2019s a <I>good<\/I> thing. Because Exec creates an object we can then work with the properties of that object, properties like StdOut and <B>Status<\/B>. And that\u2019s the key to creating this script. For example, take a look at the next block of code in our script:<\/P><PRE class=\"codeSample\">Do Until objExec.Status\n    Wscript.Sleep 250\nLoop\n<\/PRE>\n<P>What we\u2019re doing here is waiting until the Status property of the WshScriptExec object is true. If Status is false (that is, equal to 0) that means that Exec is still running the command; in other words, net localgroup Administrators hasn\u2019t finished doing whatever it is that net localgroup Administrators does. (We\u2019re just kidding: it lists the members of the local Administrators group.) If Status is false we pause the script for 250 milliseconds (<B>Wscript.Sleep 250<\/B>), then loop around and check the value of the Status property again. This continues until Status is no longer equal to 0, which can mean only one thing: our command is finished.<\/P>\n<P>As we noted earlier, when Exec runs a command-line utility that utility runs in a hidden window; in addition, any information returned by that utility is stored in Exec\u2019s StdOut property. Logically enough, that means we can report back the results of running net localgroup Administrators simply by doing this:<\/P><PRE class=\"codeSample\">Wscript.Echo objExec.StdOut.ReadAll()\n<\/PRE>\n<P>So what are we doing here? We\u2019re just using the ReadAll() method to read the value of StdOut, a value we then echo back to the screen. The net result? Although we don\u2019t see any command windows or any other signs of activity, net localgroup Administrators runs in the background; when it\u2019s done processing, the list of group members is displayed in the command window. Simple <I>and<\/I> elegant. <\/P>\n<P>We hope that answers your question, AA. Oh: and see you tomorrow, June!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I hide the command window when executing a command like net localgroup Administrators?&#8212; AA Hey, AA. You know, before we begin we\u2019d like to take a few minutes to recognize all those faithful readers who read the Hey, Scripting Guy! column each and every day. Hey, June; how\u2019s it going? [&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,714],"class_list":["post-67283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-wshshell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I hide the command window when executing a command like net localgroup Administrators?&#8212; AA Hey, AA. You know, before we begin we\u2019d like to take a few minutes to recognize all those faithful readers who read the Hey, Scripting Guy! column each and every day. Hey, June; how\u2019s it going? [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67283","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=67283"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67283\/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=67283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}