678

What command can I put at the end of a batch file to prevent auto-closing of the console after the execution of the file?

3
  • 6
    It would be good if there is a way to detect if a .cmd file was started by doubleclick in the explorer (then you want it to pause at the end) or if it was started by a already open command line. In the later case (and especially if executed by another program) you dont want the pause. I havent found a good solution for this case (and typically use a if %NOPAUSE% variable, but this only works for parent programs not for paremt shells).
    – eckes
    Nov 6, 2012 at 17:34
  • 1
    There is a way: stackoverflow.com/questions/3551888/…
    – EM0
    May 17, 2017 at 9:57
  • If pause or cmd /k doesnt work, \ add call to your command \ -- > if the command before has any type of exit code (even on success) in it then it seems to close the window, \ -- ref: stackoverflow.com/questions/4666045/…
    – Nor.Z
    Dec 28, 2023 at 2:44

21 Answers 21

820

In Windows/DOS batch files:

pause

This prints a nice "Press any key to continue . . . " message

Or, if you don't want the "Press any key to continue . . ." message, do this instead:

pause >nul
6
  • 28
    None of the suggestions here work for me when double-clicking the bat file. It's a server environment (and I have to run the script in it), so they could have modified the behavior. That doesn't change my goal. To keep the command window open so I can so the error message it types. Oct 28, 2016 at 7:28
  • 2
    Yeah this doesn't always work depending on the environment. For a more robust solution see my answer below. Essentially just do set /p exitkey= "Press any key to continue..." Sep 29, 2017 at 4:08
  • 1
    Be careful, if someone run your script without the console window (for example through the vbscript), then it would hang until you kill it through the Task Manager.
    – Andry
    Jul 1, 2019 at 21:01
  • 1
    If its something like maven use CALL, stackoverflow.com/a/52205298/4485678
    – Kevin Roy
    Aug 6, 2019 at 9:23
  • For me only worked adding: & pause Sep 14, 2023 at 10:57
185

Depends on the exact question!

Normally pause does the job within a .bat file.

If you want cmd.exe not to close to be able to remain typing, use cmd /k command at the end of the file.

3
  • 7
    If you're running Windows commands through the Run dialog, put cmd /k before your command instead. & cmd /k won't work.
    – Noumenon
    May 6, 2018 at 13:10
  • I am confused by the "at the end of the file" comment. Wouldn't we want to include the /k wherever we call the cmd that closes, as @Noumenon suggests? Admittedly, this is getting closer to what the OP asked, but expanding the scope of the question, I don't think it would need to be at the end of the file. Oct 24, 2023 at 17:33
  • 1
    @JosiahYoder docs is unclear, base on my tests (can be wrong): \ If you are running multiple command in a .bat file, cmd /k should be placed at end, with no arg. \ Otherwise, all the following cmd below cmd /k seems wont be executed. \ If you only run a single command, I think you do place at front, like cmd /k <command>.
    – Nor.Z
    Dec 28, 2023 at 2:42
95

If you want cmd.exe to not close, and able to continue to type, use cmd /k

Just felt the need to clarify what /k does (from windows website):

/k : Carries out the command specified by string and continues.

So cmd /k without follow up command at the end of bat file will just keep cmd.exe window open for further use.

On the other hand pause at the end of a batch file will simply pause the process and terminate cmd.exe on first button press

0
69

If you are using Maven and you want to skip the typing and prevent the console from close to see the result you need to use the CALL command in the script, besides just the 'mvn clean install'.

Like this will close the console

ECHO This is the wrong exemple
mvn clean install
pause

Like this the console will stay open

ECHO This is the right exemple
CALL mvn clean install
pause

If you dont use the CALL command neither of the pasts exemples will work. Because for some reason the default behaviour of cmd when calling another batch file (which mvn is in this case) is to essentially replace the current process with it, unlike calling an .exe

6
  • 2
    Thanks a lot! It prevents prompt from closing if command failed. Jul 3, 2019 at 8:15
  • 2
    It worked for me. I wanted to execute 2 differents command in 1 bat file. but my prompt closed after the first command. Putting call <myCommand> resolved this problem : My prompt stoped closing after the first execution
    – Zombkey
    Jul 18, 2019 at 9:47
  • 1
    but why does it close without CALL @andrey
    – Priyam
    Jun 26, 2020 at 11:13
  • I dont remember it anymore xD but i know for sure that have a explanation, i just dont remember and i cant find it easily on google :/ @Priyam Aug 13, 2020 at 19:15
  • 2
    @Priyam because for some reason the default behaviour of cmd when calling another batch file (which mvn is in this case) is to essentially replace the current process with it, unlike calling an .exe Oct 15, 2020 at 9:27
33

The below way of having commands in a batch file will open new command prompt windows and the new windows will not exit automatically.

start "title" call abcd.exe param1 param2  
start "title" call xyz.exe param1 param2
1
  • 2
    In my case, I had some error during run the terminal command, and so, the 'pause 'and the 'cmd /k' commands didn't helped me to stop the program 'cause of the error. This example helped me to show, I have error and also stopped to quite from terminal. Thanks! Oct 8, 2018 at 12:03
19

Add cmd.exe as a new line below the code you want to execute:

c:\Python27\python D:\code\simple_http_server.py

cmd.exe
3
  • This is exactly what i was looking for, thanks! The "pause" is just not going to work when your python script get crashed. What I wanted is to see an error message. Until now I had to run my python scripts through the console
    – user70960
    May 6, 2020 at 12:52
  • 1
    But this works fine: python script.py %1 && cmd
    – user70960
    May 6, 2020 at 13:01
  • Thank you! This helped! I was creating a batch file to run python applications.
    – mime
    Jun 5, 2020 at 3:02
14

my way is to write an actual batch (saying "foo.bat") to finish the job; then create another "start.bat":

@echo off
cmd /k foo.bat

I find this is extremely useful when I set up one-time environment variables.

1
  • 3
    You can just put cmd /k (without any command) at the end of foo.bat file and run that. You don't need a separate start.bat file.
    – ADTC
    Oct 16, 2017 at 0:13
11

Call cmd at the end of the batch file.

0
5

besides pause.

set /p=

can be used .It will expect user input and will release the flow when enter is pressed.

or

runas /user:# "" >nul 2>&1

which will do the same except nothing from the user input will be displayed nor will remain in the command history.

5

This little hack asks the user to enter a key and stores it into the variable %exitkey% (this variable could be called anything you like though).

set /p exitkey= "Press any key to continue..."

NB: the space after the '=' is very important

5

Had problems with the answers here, so I came up with this, which works for me (TM):

cmd /c node_modules\.bin\tsc
cmd /c node rollup_build.js
pause
4

I know I'm late but my preferred way is:

:programend
pause>nul
GOTO programend

In this way the user cannot exit using enter.

1
  • I think yes, as it is an enhanced way to prevent the console from closing. Even if the user press a key, the console remains open. The user will be forced to exit only by closing the console window.
    – decadenza
    Nov 11, 2018 at 19:22
4

add pause (if you don't want anything else to show up add) >nul

it should look like

@echo off
title nice
echo hello
pause >nul

all you will see is hello

3

Possibility 1: Just make 2 .bat files and write into the first:

start <filename> // name of 2nd batch file
exit

Batch file 2 is the file that wont close in the end. So now when you open batch nr.1 It will start the 2nd and cloe itself. When the 2nd finishes it will not close entirely (as long as you wont put exit at the end)

Possibility 2: Batch file 1:

call <filename>
cls
echo End of file
pause
<any code you want>

When the 2nd file ends then it will proceed to file 1 again and output the rest of it. With that you can even make error handlers. If nr.1 crashes it goes into nr.2 and displays it

0
3

There are two ways to do it depend on use case

1) If you want Windows cmd prompt to stay open so that you can see execution result and close it afterwards; use

pause

2) if you want Windows cmd prompt to stay open and allow you to execute some command afterwords; use

cmd

enter image description here

3

Run the .exe file and then pause the cmd

batch script example :

@echo off
myProgram.exe
PAUSE

batch script example with arguments :

@echo off
myProgram.exe argumentExample1 argumentExample2
PAUSE

I added @echo off because I don't want to show C:\user\Desktop>myProgram.exe and C:\user\Desktop>PAUSE in the cmd

2
pause

or

echo text to display
pause>nul
1

Easy, add cmd to your last line of bat, BUT! if you reset or clear your system path, you must start your cmd with the full path, like:

%windir%\system32\cmd.exe

For example, I have a bat file to reset jdk to old version like this:

PATH=C:\Program Files\Java\jdk1.6.0_45\bin;C:\apache-ant-1.7.1\bin
SET JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45
%windir%\system32\cmd.exe

since I reset the system path, I have to run cmd with the full path, or the system can't find cmd.exe, it will fail to run cmd, and just close the window, and you can't see the error msg.

0

cmd /k cd C:\Projects.....

If you want your cmd opened at specific long location

0

I personally put pause >nul and it waits for a key to be pressed without showing any extra text in the console.

0

using : call yourbatch.cmd

does the job will process the script and then continue ejecuting other code on same window (cmd instance)

Not the answer you're looking for? Browse other questions tagged or ask your own question.