Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My problem is that in Windows, there are command line windows that close immediately after execution. To solve this, I want the default behavior to be that the window is kept open. Normally, this behavior can be avoided with three methods that come to my mind:

  1. Putting a pause line after batch programs to prompt the user to press a key before exiting
  2. Running these batch files or other command line manipulating tools (even service starting, restarting, etc. with net start xy or anything similar) within cmd.exe(Start - Run - cmd.exe)
  3. Running these programs with cmd /k like this: cmd /k myprogram.bat

But there are some other cases in which the user:

  1. Runs the program the first time and doesn't know that the given program will run in Command Prompt (Windows Command Processor) e.g. when running a shortcut from Start menu (or from somewhere else), OR
  2. Finds it a little bit uncomfortable to run cmd.exe all the time and doesn't have the time/opportunity to rewrite the code of these commands everywhere to put a pause after them or avoid exiting explicitly.

I've read an article about changing default behavior of cmd.exe when opening it explicitly, with creating an AutoRun entry and manipulating its content in these locations:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor\AutoRun

(The AutoRun items are _String values_...)

I put cmd /d /k as a value of it to give it a try, but this didn't change the behaviour of the stuffs mentioned above at all... It just changed the behaviour of the command line window when opening it explicitly (Start-Run-cmd.exe).

So how does it work? Can you give me any ideas to solve this problem?

share|improve this question
Your question with all the explanations made my head want to explode. Can you say something like: I want to run a batch file with this "example" command, and I want the command box to stay open when finished. Is that what you are asking? Please put in a close sample of the code. – KCotreau Jul 4 '11 at 15:40
+1 Agreed. A little to verbose, please reconsider your question and, if possible, use an example and compose a more succinct question. – slotishtype Jul 4 '11 at 15:47
This is normal behaviour, and while your question certainly is valid, you should be questioning why you need to keep the command prompt open after (most utilities are created with this caveat in mind). – Breakthrough Jul 4 '11 at 16:16
In my opinion, this is not a real question. As you just instruct the user to use a batch file instead. If that's not the intent of the question, please clarify your question by removing any confusing information and explain what your real goal is. It's hard to come up with the right answer if it's unclear... – Tom Wijsman Jul 4 '11 at 17:21
@Randolf Richardson, that's because those tools aren't supposed to be seen by an end-user ever (after all, that's why we use GUIs now). If you did need such information, most IT groups will send out batch files to gather them the appropriate system information and display it. I can see no possible way that if a solution to this question is found, it will cause less problems then it solves - so many things are sent to stdout on a constant basis which were never meant to be visible for a good reason. – Breakthrough Jul 4 '11 at 18:55

12 Answers 12

Quote Microsoft:

A console is closed when the last process attached to it terminates or calls FreeConsole.

In other words, Win32 console window will always be closed when the last program running inside it exits, and you cannot change this.

(For 16-bit MS-DOS programs, Windows offers an option "Close on exit" in the properties dialog, but this is not an exception to the behavior above: it's the NTVDM process that holds the window open. Also, this option is again per-program.)

share|improve this answer
+1 for understanding how and why console windows close (as opposed to most who just say "OMG WHY CAN'T IT STAY OPEN!!!!!!!!!!!1111one"). It's behaving as designed, and it should stay that way IMHO. This is the primary reason why we have standard streams and batch files in the first place. – Breakthrough Jul 4 '11 at 19:29
@Breakthrough The question is How can I do X, not In your opinion, should I do X. This answer is unhelpful, it implies X is impossible, when partial solutions have already been presented elsewhere. (-1) – Superbest Jun 9 '14 at 18:07

I have a solution which can only apply on .cmd and .bat files:

Goto HKCR\cmdfile\shell\open\command and change the "Default key value" to cmd.exe /k "%1" %. Also do this with HKCR\batfile\shell\open\command. Now, every batch script window will stay open after its execution.

Note that this is like using cmd.exe /c cmd.exe /k program.bat, meaning that another CMD instance will be launched into the parent one. I couldn't find how to overwrite the first /c argument.

You can also do so with [exefile], but now it'll show an empty console box if the executable has a GUI.

share|improve this answer
+1 for having the only solution here which actually answers the question given. Unfortunately, I think this may be a memory leak waiting to happen. What happens when a service launches a new batch file without specifying CMD.EXE in the CMD line? Won't this code be used in that case? Isn't this going to create a new window on a non interactive desktop and leave it open until you restart? – krowe2 Nov 20 '14 at 20:10

Just open a command prompt at the location of your batch file, and manually key in the name of your batch file to run it within that window.

1.Navigate to the folder where your executable resides
2.Shift-Right click and select "Command Window from here"
3.type in the name of the executable and hit enter
4.The process should run, but the window should stay open

share|improve this answer
I didn't know about Shift+Right click. Also, however, you can drag-drop the exe into a cmd window, which automatically puts in the path for you. – Superbest Jun 9 '14 at 18:08

cmd.exe /k will give you trouble with some batch. If the batch exit 1 (without /B) your console will be closed. A trick is to use:

cmd.exe /k cmd /c ...

To use this by default for all batch files, set HKEY_CLASSES_ROOT\cmdfile\shell\open\command\(default) and HKEY_CLASSES_ROOT\batfile\shell\open\command\(default) to "%windir%\system32\cmd.exe" /k "%windir%\system32\cmd" /c "%1" %*.

share|improve this answer
I thought that "%*" passes the full parameter list, so wouldn't this pass the first parameter twice, once as "%1" and again as the first parameter of "%*" ? – Kevin Fegan Mar 21 '13 at 18:49
@KevinFegan I haven't checked the reason but %1 should keep the quotes which means it would be possibly double quoted in some cases. There is also a difference between %1 %2 %3 .. and %* which I don't remember right now but the %1 %2 %3 works in some cases where %* fails. – Wernight Apr 26 '13 at 8:28

Rather than using PAUSE (I prefer CHOICE or TIMEOUT to using PAUSE when necessary), you can use the solution here: 886848/how-to-make-windows-batch-file-pause-when-double-clicked.

This way, if you click on a batch file or link (shortcut) from within Windows Explorer, you can have the batch file program pause before it exits so you can view the program output and any error messages.

Using CHOICE, you can program the ability for the user to select a variety of actions to take, and using TIMEOUT can let the window close after a time delay (unattended). If you run the batch file from within a command prompt window, these pauses can be annoying.

The linked solution will let you decide if you want to skip the pauses if the batch file was run from a command prompt window.

You can read more at the provided link, but the basics of it is that it uses the environment variable:


to determine if the batch file was run from a command window or not.

So, you use it like this:

At the point where the batch file will exit, you add some code like this:

echo %cmdcmdline:"=-% | find /i "cmd /c --%~dpf0%-"
if %errorlevel% NEQ 0 goto :EOF
goto :EOF

rem if %errorlevel% EQU 0 batch-file: %~dpf0 was executed from Windows Explorer, ...
rem if %errorlevel% NEQ 0 batch-file: %~dpf0 was executed from within a Command Prompt
share|improve this answer

I just hit on a dumb solution after reading grawity's answer.

My use case was setting up a console environment at work where all the more reasonable solutions are blocked. All I need is to set PATH and configure some doskey aliases and be dumped to the shell.

My solution (just tested on Win7) as add cmd as the last line in the batch file. This runs a nested command prompt that inherits the environment of its parent. That child shell holds the batch process open until you EXIT, at which point the batch has no child processes and also exits.

share|improve this answer

The best way I have found to get around this is to call your batch files from another batch file using start.

so if you normally run a file called go.bat, then you would create a second batch file named something like go2.bat

the contents of go2.bat would just be

start go.bat

the first window closes immediately, but the one running your go.bat remains open

share|improve this answer

What I generally do is just like in c where we put getchar, I put the following code

set /p Var1="Prompt String" <br/>
echo %Var1% <br/>
pause <br/>

This will close only when enter button or some key board stroke is given

share|improve this answer
This answer was already covered in the question. – DavidPostill Apr 28 at 8:10

This works for version 5.00 of the Windows Registry Editor.

@="cmd.exe /k \"%1\" %*"

@="cmd.exe /k \"%1\" %*"

Save the above cmds in a file called something like cmdbatfile-open.reg and execute it!

share|improve this answer
Welcome to Stack Exchange! I've fixed the formatting in your answer for you. See here for the formatting help. Also, when you post code, please remember to explain what exactly it does. You may also want to read the how-to on answering. – Blacklight Shining Sep 24 '13 at 12:55

Suppose you have two batch files you want to launch in their own instance and keep open if their executable exits. Lets call those two batches A.bat and B.bat.

Now for simplicity you could have a batch file to launch them both, let's call that launch_both.bat with contents:

start cmd /k "A.bat & cmd /c"
start cmd /k "B.bat & cmd /c"

It might not work as expected if you call exit in one of the two batch files. That will need to be tested.

share|improve this answer

Just press the Shift button while starting the program, and the cmd window will stay open.

share|improve this answer
Just tried, it didn't. – Superbest Jun 9 '14 at 18:02

Add & pause to last command in your bat file.

share|improve this answer
This answer was already covered in the question. – Ro Yo Mi Jan 20 at 5:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .