75

I have this bat file and I want to minimize the cmd window when I run it:

@echo off
cd /d C:\leads\ssh 
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb

I want the command window minimized immediately. Any ideas on how to do this?

Arnav Thorat
  • 3,078
  • 3
  • 8
  • 33
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321

14 Answers14

120

There is a quite interesting way to execute script minimized by making him restart itself minimised. Here is the code to put in the beginning of your script:

if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
... script logic here ...
exit

How it works

When the script is being executed IS_MINIMIZED is not defined (if not DEFINED IS_MINIMIZED) so:

  1. IS_MINIMIZED is set to 1: set IS_MINIMIZED=1.
  2. Script starts a copy of itself using start command && start "" /min "%~dpnx0" %* where:

    1. "" - empty title for the window.
    2. /min - switch to run minimized.
    3. "%~dpnx0" - full path to your script.
    4. %* - passing through all your script's parameters.
  3. Then initial script finishes its work: && exit.

For the started copy of the script variable IS_MINIMIZED is set by the original script so it just skips the execution of the first line and goes directly to the script logic.

Remarks

  • You have to reserve some variable name to use it as a flag.
  • The script should be ended with exit, otherwise the cmd window wouldn't be closed after the script execution.
  • If your script doesn't accept arguments you could use argument as a flag instead of variable:

    if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit or shorter if "%1" == "" start "" /min "%~f0" MY_FLAG && exit

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
izzekil
  • 5,781
  • 2
  • 36
  • 38
  • 2
    add "not", eq, -> if not DEFINED – sss Jan 28 '15 at 11:47
  • Thanks for noticing this. A genuine typo! – izzekil Feb 02 '15 at 18:15
  • 1
    thanks, good one! I wanted to kill a process when the user logs in, so I dropped a bat like this in the Startup folder without being too intrusive. – dmihailescu Mar 25 '15 at 15:03
  • I used it for exactly the same purpose :) I was killing some annoying service that popped up on every login to server and I had no permissions to wipe it out from startup sequence. – izzekil Mar 26 '15 at 00:19
  • reminds me of the exclusion guards in C. My first year CS professor had a project to show us the value of those guards..... – John Kraemer Aug 24 '17 at 23:32
  • This helped me solve a very tricky problem: https://stackoverflow.com/questions/48175160/can-i-make-node-js-opn-open-the-browser-with-maximized-window-when-node-js-itsel/48204548#48204548. For some reason this works for my case. Thanks a lot for this one. – sbtpr Jan 11 '18 at 10:18
  • This works reasonably well for a scheduled task, though I'm unsure if the temporary console window can momentarily steal focus. So far so good... – bambams Jan 17 '20 at 18:31
  • does this forwards any arguments from the first batch? it didn't work for me that way? only getting:The filename, directory name, or volume label syntax is incorrect. – JoBe Oct 09 '20 at 08:45
  • `If your script doesn't accept arguments you could use argument as a flag instead of variable:` I don't understand this part at all, could someone please elaborate? How do you read the shorten code? – Unknown123 Oct 15 '22 at 18:38
57

Use the start command, with the /min switch to run minimized. For example:

start /min C:\Ruby192\bin\setrbvars.bat

Since you've specified a batch file as the argument, the command processor is run, passing the /k switch. This means that the window will remain on screen after the command has finished. You can alter that behavior by explicitly running cmd.exe yourself and passing the appropriate switches if necessary.

Alternatively, you can create a shortcut to the batch file (are PIF files still around), and then alter its properties so that it starts minimized.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
26

The only way I know is by creating a Windows shortcut to the batch file and then changing its properties to run minimized by default.

Bruno Silva
  • 3,077
  • 18
  • 20
24

Using PowerShell you can minimize from the same file without opening a new instance.

powershell -window minimized -command ""

Also -window hidden and -window normal is available to hide completely or restore.


source: https://stackoverflow.com/a/45061676/1178975

user1cat
  • 341
  • 2
  • 3
  • Powershell introduce delays. Any idea to make it faster? – Unknown123 Sep 20 '19 at 13:05
  • Oh wow, the hidden window is amazing! Can't do that with a Windows shortcut. – JonathanDavidArndt Jan 20 '22 at 19:42
  • This saved me, thanks! I wanted to unlock a bitlocker container, then launch an app and lock it after the app is closed. So a bat file run as admin asks for container password, then runs second batch using your PS that starts an encrypted app and waits for it to finish to relock. And unlike other tools it passes the elevated privileges to the second batch to properly lock. – NIKER Nov 22 '22 at 23:17
8

If you want to start the batch for Win-Run / autostart, I found I nice solution here https://www.computerhope.com/issues/ch000932.htm & https://superuser.com/questions/364799/how-to-run-the-command-prompt-minimized

cmd.exe /c start /min myfile.bat ^& exit
  • the cmd.exe is needed as start is no windows command that can be executed outside a batch
  • /c = exit after the start is finished
  • the ^& exit part ensures that the window closes even if the batch does not end with exit

However, the initial cmd is still not minimized.

Peter T.
  • 2,927
  • 5
  • 33
  • 40
7

One way to 'minimise' the cmd window is to reduce the size of the console using something like...

echo DO NOT CLOSE THIS WINDOW
MODE CON COLS=30 LINES=2

You can reduce the COLS to about 18 and the LINES to 1 if you wish. The advantage is that it works under WinPE, 32-bit or 64-bit, and does not require any 3rd party utility.

SSi
  • 427
  • 4
  • 5
3

If you type this text in your bat file:

start /min blah.exe

It will immediately minimize as soon as it opens the program. You will only see a brief flash of it and it will disappear.

Community
  • 1
  • 1
Kyroflash
  • 31
  • 1
  • Thanks for you contribution, but this is the same approach as the highest voted answer. – yhw42 Sep 27 '13 at 12:44
2

One option is to find one of the various utilities that can change the window state of the currently running console window and make a call to it from within the batch script.

You can run it as the first thing in your batch script. Here are two such tools:

min.exe http://www.paulsadowski.com/wsh/cmdprogs.htm

cmdow http://www.commandline.co.uk/cmdow/index.html

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Bernard Chen
  • 6,437
  • 5
  • 23
  • 27
  • 1
    I usually like to use `nircmdc.exe` for stuff like this but for some reason it wouldn't minimize the active window when it was a console window. However, `cmdow` worked great! `cmdow @ /min` was how I did it, where `@` is how to indicate that you want to target the active window. (I'm in Windows 10.) – Craig Silver Oct 15 '16 at 15:22
1

Yet another free 3rd party tool that is capable of minimizing the console window at any time (not only when starting the script) is Tcl with the TWAPI extension:

echo package require twapi;twapi::minimize_window [twapi::get_console_window] | tclkitsh -

here tclkitsh.exe is in the PATH and is one of the tclkit-cli-*-twapi-*.exe files downloadable from sourceforge.net/projects/twapi/files/Tcl binaries/Tclkits with TWAPI/. I prefer it to the much lighter min.exe mentioned in Bernard Chen's answer because I use TWAPI for countless other purposes already.

Community
  • 1
  • 1
robert4
  • 1,072
  • 15
  • 20
1

You can minimize the command prompt on during the run but you'll need two additional scripts: windowMode and getCmdPid.bat:

@echo off

call getCmdPid
call windowMode -pid %errorlevel% -mode minimized

cd /d C:\leads\ssh 
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb
npocmaka
  • 55,367
  • 18
  • 148
  • 187
1

You could try running a script as follows

var WindowStyle_Hidden = 0
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c setrbvars.bat", WindowStyle_Hidden)

save the file as filename.js

PatchedUp
  • 41
  • 3
  • 2
    The request is to hide the command prompt from a *batch file*, not make a javascript to run elsewhere that runs something else which finally hides it. – ssube Feb 11 '12 at 13:57
0

Another option that works fine for me is to use ConEmu, see http://conemu.github.io/en/ConEmuArgs.html

"C:\Program Files\ConEmu\ConEmu64.exe" -min -run myfile.bat
Peter T.
  • 2,927
  • 5
  • 33
  • 40
0

try these

CONSOLESTATE /Min

or:

SETCONSOLE /minimize

or:

TITLE MinimizeMePlease
FOR /F %%A IN ('CMDOW ˆ| FIND "MinimizeMePlease"') DO CMDOW %%A /MIN
Dominique
  • 16,450
  • 15
  • 56
  • 112
0

http://conemu.github.io/en/ConEmuArgs.html download flagged by Virus Total. May have Malware.

Gary
  • 11
  • 1