260

I have a bat file like this:

ipconfig

That will print out the IP info to the screen, but before the user can read that info CMD closes itself.

I believe that CMD assumes the script has finished, so it closes.

How do I keep CMD open after the script is finished?

5
  • 1
    Dupe of stackoverflow.com/questions/988403/…
    – Rob Kielty
    Jul 30, 2013 at 20:51
  • 5
    @RobKielty it's the same question alright - but here we have a new answer (which is better IMHO)
    – Nir Alfasi
    Jul 30, 2013 at 20:57
  • 1
    possible duplicate of How to keep cmd running after opening a .bat file script
    – Endoro
    Jul 30, 2013 at 20:58
  • @alfasin Is that not the same as Rutger Nijlunsing's answer?
    – Rob Kielty
    Jul 30, 2013 at 20:59
  • @RobKielty nope, my answer isn't the same as it doesn't use pause which is a nice workaround (and that's why I upvoted this answer) but not one that really solves the issue because after the user hits Enter or Space the window will close...
    – Nir Alfasi
    Jul 30, 2013 at 21:01

12 Answers 12

343

Put pause at the end of your .BAT file.

2
  • 13
    If you still want to interact with the bat file after your execution, replace the PAUSE with just cmd /k - then after your scripts have run, you can still run manual commands afterwards in the same context Nov 13, 2018 at 14:39
  • 1
    If the last command fails pause won't work. As a workaround one can use call before the last command to ensure pause works. Oct 12, 2023 at 10:16
243

Depending on how you are running the command, you can put /k after cmd to keep the window open.

cmd /k my_script.bat

Simply adding cmd /k to the end of your batch file will work too. Credit to Luigi D'Amico who posted about this in the comments below.

4
  • 23
    After all your scripts in your bat file - put --> cmd /k Nov 13, 2018 at 14:39
  • 9
    Beware that if your program contains syntactical errors, the window will still close if you ran it via double-click and put cmd /k at the end. Jun 25, 2019 at 6:47
  • 2
    This doesn't explain cmd windows closing if the user double clicked the file.
    – basickarl
    Feb 13, 2020 at 12:43
  • This command seems to reset my shell. If use it in a 'Git Bash' shell (a modified cmd prompt that came with my git installation) on windows, the the command prompt is reverted to the standard version and won't remember the the commands I typed in... Is there a way to get around this?
    – TheBaum
    Jun 13, 2022 at 12:13
76

Just add @pause at the end.

Example:

@echo off
ipconfig
@pause

Or you can also use:

cmd /k ipconfig
5
30

When the .bat file is started not from within the command line (e.g. double-clicking).

echo The echoed text
@pause

at_pause

echo The echoed text
pause

pause

echo The echoed text
cmd /k

cmd k

echo The echoed text & pause

and_pause

12

Adding pause in (Windows 7) to the end did not work for me
but adding the cmd /k in front of my command did work.

Example :

cmd /k gradlew cleanEclipse
6

start cmd /k did the magic for me. I actually used it for preparing cordova phonegap app it runs the command, shows the result and waits for the user to close it. Below is the simple example

start cmd /k echo Hello, World!

What I did use in my case

start cmd /k cordova prepare

Update

You could even have a title for this by using

start "My Title" echo Hello, World!

2

In Windows add '& Pause' to the end of your command in the file.

1

If you are starting the script within the command line, then add exit /b to keep CMD opened

1
  • Doesn't practically solve the question as modifying ipconfig and other built-in batch files is a very bad idea.
    – robinCTS
    Nov 14, 2017 at 3:22
1

I was also confused as to why we're adding a cmd at the beginning and I was wondering if I had to open the command prompt first. What you need to do is type the full command along with cmd /k. For example assume your batch file name is "my_command.bat" which runs the command javac my_code.java then the code in your batch file should be:

cmd /k javac my_code.java

So basically there is no need to open command prompt at the current folder and type the above command but you can save this code directly in your batch file and execute it directly.

1

Putting "call" in front of my last command followed by a "cmd /k" command worked for me.

call mvn clean package
cmd /k
0
javac -d C:\xxx\lib\ -classpath C:\xxx\lib\ *.java

cmd cd C:\xxx\yourbat.bat

the second command make your cmd window not be closed. The important thing is you still able to input new command

0

As a sidenote this also works when running a command directly from the search bar in windows.

e.g. directly running ipconfig will directly close the cmd window after the command has exited.

enter image description here

Using cmd \k <command> won't - which was what i was trying to do when i found this answer.

enter image description here

It has the added advantage of always recognizing the command you're trying to run. E.g. running echo hello world from the searchbar won't work because that is not a command, however cmd \k echo hello world works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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