How do I minimize the command prompt from my bat file

227,853

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
227,853
Matt Elhotiby
Author by

Matt Elhotiby

Interests: Javascript, React and Rails

Updated on July 05, 2022

Comments

  • Matt Elhotiby
    Matt Elhotiby almost 2 years

    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?

  • Matt Elhotiby
    Matt Elhotiby about 12 years
    i tried this and it does hide the cmd window but it doesnt run the next line ruby C:\leads\ssh\put_leads.rb
  • Cody Gray
    Cody Gray about 12 years
    @Tamer: No, of course it doesn't. It only runs the command specified as an argument to the start command. Put all of the commands inside of the batch file, or call start multiple times.
  • ssube
    ssube about 12 years
    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.
  • Dennis Jaheruddin
    Dennis Jaheruddin almost 11 years
    Was a lot easier than expected.
  • joachim stephen
    joachim stephen over 10 years
    Thanks for you contribution, but this is the same approach as the highest voted answer.
  • macetw
    macetw about 10 years
    Unfortunately, I cannot do this with a scheduled task. The scheduled tasks does not seem to recognize the "start" as a valid command.
  • sss
    sss over 9 years
    add "not", eq, -> if not DEFINED
  • izzekil
    izzekil about 9 years
    Thanks for noticing this. A genuine typo!
  • dmihailescu
    dmihailescu about 9 years
    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.
  • izzekil
    izzekil about 9 years
    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.
  • Craig Silver
    Craig Silver over 7 years
    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.)
  • John Kraemer
    John Kraemer over 6 years
    reminds me of the exclusion guards in C. My first year CS professor had a project to show us the value of those guards.....
  • sbtpr
    sbtpr over 6 years
    This helped me solve a very tricky problem: stackoverflow.com/questions/48175160/…. For some reason this works for my case. Thanks a lot for this one.
  • Business Tomcat
    Business Tomcat about 6 years
    Only way that worked for me to start a batch file minimized via Windows Task Scheduler.
  • Ε Г И І И О
    Ε Г И І И О almost 6 years
    @macetw How about putting that in another batch file?
  • Unknown123
    Unknown123 over 4 years
    Powershell introduce delays. Any idea to make it faster?
  • bambams
    bambams over 4 years
    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...
  • JoBe
    JoBe over 3 years
    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.
  • federico verchez
    federico verchez over 3 years
    i agree with dennis' comment. as they say, 'keep it simple sam'. no code needed, no sweat
  • Bosco Domingo
    Bosco Domingo almost 3 years
    As @BusinessTomcat said, this is the only one that seems to work properly. Thanks!!
  • JonathanDavidArndt
    JonathanDavidArndt over 2 years
    Oh wow, the hidden window is amazing! Can't do that with a Windows shortcut.
  • Susaj S N
    Susaj S N almost 2 years
    the hack, thanks mate much easier steps :)