[windows] How do I minimize the command prompt from my bat file

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

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

This question is related to windows command-line batch-file cmd command-prompt

The answer is


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.


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.


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

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.


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


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.


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


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

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


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.


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.


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


Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)

Examples related to batch-file

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH CanĀ“t run .bat file under windows 10 Execute a batch file on a remote PC using a batch file on local PC Windows batch - concatenate multiple text files into one How do I create a shortcut via command-line in Windows? Getting Error:JRE_HOME variable is not defined correctly when trying to run startup.bat of Apache-Tomcat Curl not recognized as an internal or external command, operable program or batch file Best way to script remote SSH commands in Batch (Windows)

Examples related to cmd

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH VSCode Change Default Terminal How to install pandas from pip on windows cmd? 'ls' in CMD on Windows is not recognized Command to run a .bat file VMware Workstation and Device/Credential Guard are not compatible How do I kill the process currently using a port on localhost in Windows? how to run python files in windows command prompt?

Examples related to command-prompt

CMD command to check connected USB devices How do I kill the process currently using a port on localhost in Windows? PowerShell The term is not recognized as cmdlet function script file or operable program open program minimized via command prompt How to connect to SQL Server from command prompt with Windows authentication How to see the proxy settings on windows? How do I type a TAB character in PowerShell? Batch file to split .csv file Aliases in Windows command prompt Change all files and folders permissions of a directory to 644/755