• ARTICLES
SEARCH

How-To Geek

Can I Run a Windows Batch File without a Visible Command Prompt?

Batch files are a handy way to execute a series of commands in Windows, but is there anyway to run them invisibly in the background? Read on find out how.

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

The Question

SuperUser reader Jake wants to run a BAT file invisibly, he writes:

I have installed a ruby gem called Redcar, which is launched from the command line. When it runs, it steals the shell until it terminates, so I have to create a new shell window to continue doing command line work. The shell I’m using is the GITBash shell from MySysGit.

I found a Redcar.bat file which is meant to launch Redcar as a shortcut, I presume, but I don’t want the extra command prompt window to open whenever I launch the BAT file.

How do I just run the BAT without seeing the prompt?

Is there a solution for Jake’s stealthy prompt desire?

The Answers

SuperUser contributor Afrazier responds with a combination of bad and good news:

You can’t — executing a batch file with the built in Command Prompt is going to keep a window open until the batch file exits.

What you can do is take steps to make sure that the batch file exits as quickly as possible. If at all possible, modify the batch file to run whatever program with the start command. By default, start returns immediately without waiting for the program to exit, so the batch file will continue to run and, presumably, exit immediately. Couple that with modifying your shortcut to run the batch file minimized, and you’ll only see the taskbar flash without even seeing a window onscreen.

One caveat to this is that if you’re running a console-mode program, which many script interpreters are, the batch file will wait for the program to exit, and using start will spawn a new console window. What you need to do in this case is run the Windows-based version of the interpreter instead of the console-based one — no start necessary. For Perl, you would run wperl.exe instead of perl.exe. For Python, it’s pythonw.exe instead of python.exe. The old win32 Ruby distribution I have downloaded has rubyw.exe, which should do the same thing.

A final possibility is to use a 3rd-party tool to run the command prompt with a hidden window. I’ve heard of such things but never had a use for them, so I don’t know of anything in particular to point you to.

Readers also pointed him to another SuperUser thread that highlights how you can use a Visual Basic Script to go beyond minimizing the visibility and outright hide the CMD prompt. In that thread, Harry MC explains:

Solution 1:

Save this one line of text as file invisible.vbs:

CreateObject(“Wscript.Shell”).Run “”"” & WScript.Arguments(0) & “”"”, 0, False

To run any program or batch file invisibly, use it like this:

wscript.exe “C:\Wherever\invisible.vbs” “C:\Some Other Place\MyBatchFile.bat”

To also be able to pass-on/relay a list of arguments use only two double quotes

CreateObject(“Wscript.Shell”).Run “” & WScript.Arguments(0) & “”, 0, False

eg: Invisible.vbs “Kill.vbs ME.exe”

Solution 2:

Use a command line tool to silently launch a process : Quiet.

Employing any of the above solutions, based on your comfort level using VBS and third party tools or not, will at minimum reduce the visibility of the CMD window or outright remove it.


Have something to add to the explanation? Sound off in the the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.


 

Jason Fitzpatrick is warranty-voiding DIYer and all around geek. When he's not documenting mods and hacks he's doing his best to make sure a generation of college students graduate knowing they should put their pants on one leg at a time and go on to greatness, just like Bruce Dickinson. You can follow him on if you'd like.

  • Published 01/1/13

Comments (12)

  1. rick

    Instead of a unitasking tool like Quiet, why not use one that can serve this purpose and so much more, NirCmd?
    http://www.nirsoft.net/utils/nircmd.html

    Usage:
    nircmd exec hide c:\wherever\MyBatchFile.bat

  2. Richard

    I’m a fan of your site but I have to admit that I’m not sure why you included Afrazier’s response because he was completely wrong, as shown by Harry MC.

    The internet is full of untruths already. I can’t help thinking that adding to it by reposting an incorrect answer to a question (when you could have just posted the right answer) doesn’t really help things.

  3. Don Flood

    How about using the Task Scheduler with the “Run whether the user is logged on or not” option checked? This works for me.

  4. Vinh

    A program called CMDOW can hide the command window. A word of warning, due to it’s possible abuse, the file might be flagged as a hacking tool so you may have to whitelist it from your AV.
    http://www.commandline.co.uk/cmdow/

  5. thegeekkid

    The bat to exe compiler that I use offers the option of invisible application. http://www.f2ko.de/programs.php?lang=en&pid=b2e

    I like to compile my batch files into a exe because it basically blocks someone from stealing my code if I don’t want them to. The invisible application option works quite well. :)

  6. Ronny

    “Anyway” is not the same as “Any way”.

  7. vhanla

    You can run another batch file with START /MIN CMD.EXE /C mybatchfile.bat from within the command prompt.

    From outside it will bring the cmd console window but to avoid that, you can create a shortcut file with properties modified to start minimized.

  8. Michael

    You can also use the AutoIt script language. The builtin Run command can execute your bat file and hide the cmd window. See http://www.autoitscript.com/autoit3/docs/functions/Run.htm for more details. Use the show_flag parameter set to @SW_HIDE.

  9. Henk van Setten

    If you really want to see nothing on screen at all, not even a brief flash, you should use the tiny freeware utility Nircmd by Nirsoft (a Google search will get you to the download right away).
    Nircmd has many useful parameter options to run things, such as:
    nircmd exec hide “c:\temp\test.exe” will run any exe (test, in this example) without showing anything.
    Especially handy here is this one:
    nircmd execmd “c:\temp\my.bat” will run any command prompt thingy (my.bat, in this example) without showing any console window at all.

  10. Charles

    Afrazier is dead wrong. I have been using the VBS solution to silently map network drives at startup and it works without issue.

  11. David

    Commercial solutions are also available – search for “Hidden Start”. My personal favorite is nircmd – what a gem!

  12. George

    I’ve tried what Vhanla suggested, I created a shortcut (.lnk) file for my .bat file and modified its properties (alt-enter) I set it to Minimized and it runs hidden. No need of third party tools.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!