How Can I Hide the Command Window When Executing a Command Like net Localgroup Administrators?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I hide the command window when executing a command like net localgroup Administrators?

— AA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AA. You know, before we begin we’d 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’s it going?

No, that’s pretty much it. Maybe we should have said we wanted to recognize our faithful reader.

Hmmm, that’s an interesting idea: if we actually answered the questions instead of rambling on and on maybe we would have more than just one faithful reader. What the heck; we’re willing to try anything:

Set objShell = CreateObject(“Wscript.Shell”)
strCommand = “net localgroup Administrators” 

Set objExec = objShell.Exec(strCommand)

Do Until objExec.Status Wscript.Sleep 250 Loop

Wscript.Echo objExec.StdOut.ReadAll()

To begin with, AA, we’re assuming that you’ve been using the Windows Script Host Run 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’t 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’d like your entire session to look something like this:

C:\scripts>cscript hidden.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Alias name Administrators Comment Administrators have complete and unrestricted access to the computer/domain

Members

——————————————————————————- Administrator Ken Myer kenmyer FABRIKAM\InfoSec Secure Environment FABRIKAM\Domain Admins FABRIKAM\kenmyer

The command completed successfully.

Can we do that? You bet we can, as long as we use the Exec 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 StdOut property. All we have to do is read in the value of StdOut and we’ll have complete access to the returned data.

Which is exactly what we do in our script. We start out by creating an instance of the Wscript.Shell 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:

Set objExec = objShell.Exec(strCommand)

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’s required because the Exec method actually creates an instance of an object: the WshScriptExec object.

No, that’s not a bad thing; that’s a good thing. Because Exec creates an object we can then work with the properties of that object, properties like StdOut and Status. And that’s the key to creating this script. For example, take a look at the next block of code in our script:

Do Until objExec.Status
    Wscript.Sleep 250
Loop

What we’re 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’t finished doing whatever it is that net localgroup Administrators does. (We’re just kidding: it lists the members of the local Administrators group.) If Status is false we pause the script for 250 milliseconds (Wscript.Sleep 250), 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.

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’s StdOut property. Logically enough, that means we can report back the results of running net localgroup Administrators simply by doing this:

Wscript.Echo objExec.StdOut.ReadAll()

So what are we doing here? We’re 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’t see any command windows or any other signs of activity, net localgroup Administrators runs in the background; when it’s done processing, the list of group members is displayed in the command window. Simple and elegant.

We hope that answers your question, AA. Oh: and see you tomorrow, June!

0 comments

Discussion is closed.

Feedback usabilla icon