How to avoid command window popping on cmd.exe

39,565

Solution 1

The simplest option is to start the thing minimized. Shortcut Target:

cmd /c START /MIN \path\to\test.bat

or

cmd /c START /MIN cmd /k ( ping 1.1.1.1 -w 10000 -n 1 && @ECHO All OK)

It's not hidden or anything, but it doesn't show up on the desktop or—worse—steal the focus.

If you want the window to go away on its own, "cmd /c ..." will make that happen. "cmd /k ..." will leave the window open.

Plan B is referred to by @CodyGray in the SU link he posted. There are programs that don't open windows, like wperl, pythonw, or wscript (natively available on Windows). If you can pass your command through to one of those things, then you could effectively double-click an icon and have it run "silently."

If Perl's available, I'd certainly go with that because you can craft some pretty powerful one-liners that won't require creating other files.

wperl -MWin32 -MNet::Ping -e "$p=Net::Ping->new('icmp',10000); if ($p->ping('192.168.1.1')) { Win32::MsgBox('Ping Successful', 1 + MB_OK, 'All Good'); }"

In your example, you're chaining commands together, the latter is a notification. If you don't want to have a window open for the first command, it would be awkward to do it for the second when you're notifying the user of something. Having the process call "cmd /c start cmd /c @echo Everything's OK" would probably do it, but using CMD windows for user notification is probably not something the HCI guys would smile at.

Solution 2

No, all batch files open in command-line windows; this has nothing to do with the presence of cmd.exe in your particular file. A batch file is simply a number of command-line commands, one per line.

I don't understand why you write test.bat the way you do. I'd rather expect

ping 1.1.1.1 -n 1 -w 10000
echo second command goes here

If, for some bizzare reason, you really need to use only a single line, you can simply do

ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here

Solution 3

As Andreas Rejbrand already explained, the command prompt window is not from the explicit cmd.exe invocation within your script but from executing the .bat script itself. (And despite your claim, you haven't provided any evidence why explicitly invoking cmd.exe is necessary. The whole point of a .bat script is to batch commands together.)

That said, the silentbatch program that Paul Miner and I wrote can execute batch scripts and suppress the command prompt window. To use it, you would have to create a Windows shortcut that invokes silentbatch.exe test.bat and double-click on that rather than double-clicking on test.bat directly, however.

Solution 4

You could also create a shortcut to you batch script and then in the properties select to start the app minimized. It is explained here: CNET: How to automatically start a program minimized in Windows

Step 1: Right-click on the shortcut of the program you want to start minimized and select Properties.
Step 2: Click on the drop-down menu under Run.
Step 3: Select "Minimized," then click the OK button.

Share:
39,565
sambha
Author by

sambha

Updated on May 31, 2020

Comments

  • sambha
    sambha about 4 years

    I have a command as:

    cmd.exe /c ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here
    

    But when executed it opens a command window. Is there a way to avoid the command window from popping up?

    PS: I cannot remove cmd.exe from there. As you can see, I am trying to tie two commands one after the other in the same string.

    Thanks.

    Edit: Sorry. Its not a bat file. I want to execute 2 commands in the "UninstallString" of msiexec. I was trying so many things that my question got a bit carried away.

    The command is:

    msiexec <product> <package> && reg delete /xxx