Whenever there are many set of commands that are needed to be executed, users create a batch file and runs it on the windows system. But have you noticed that the command-line tool like command prompt, which runs this batch file gets closed soon after executing the batch file? So users are not able to see the output or results of the command execution.
You might wonder if it is possible to stop the command prompt from closing immediately after execution? Yes, it is!
In this article, you will learn about how this can be done using 3 simple methods. Please continue reading if you want to know more.
Method 1 – By Adding PAUSE to the batch script file
One of the ways with which the user can stop the command prompt from closing is to add a word called Pause at the end of the script file. This makes the command prompt pause after the execution of the batch file till the user presses any key and closes as soon as a key is pressed.
Follow the steps given below on how to do it.
Step 1: Go to the folder where the batch file is present.
Step 2: Right-click on the batch file and click Show more options from the context menu.
Step 3: Then click Edit on top of the context menu as shown in the screenshot below.
Step 4: This will open the batch file in a Notepad or any other text editor app which is set as default.
Step 5: Type pause at the end of the script as a final line and save the file by pressing CTRL + S keys together on the keyboard.
Step 6: Next, double-click on the batch file or right-click on it and select Open to run this batch file.
Step 7: Now you can see that the batch script has been executed and the command prompt window is still open with the message saying “Press any key to continue . . . ” as shown in the below image.
Method 2 – Add cmd /k at the end of the batch file
If any user wants to not only stop closing the command prompt window after executing a batch file but also to run some other commands, then this method is good to try. The user has to only add cmd /k at the end which will be explained using the steps below.
Step 1: Right-click on the batch file and you should select Show more options from its context menu as shown below.
Step 2: Then click Edit from the menu as shown.
Step 3: If there is no default text editor set on the system, then it will prompt you to select any editor installed like notepad.
Step 4: Once it opens up, type cmd /k at the end of the batch script file as shown below.
Step 5: Now save the file by going to the File option on the top menu and clicking Save from the list as shown below.
Step 6: Now you can run the batch file by double-clicking on it.
Step 7: You can see that after running the batch file, the command prompt will not get closed and also allow the users to execute any other command as shown in the image below.
Step 8: This is how you can stop the command prompt from closing but you need to add either Pause or cmd /k to all the batch files that you are running.
Method 3 – Changing the Registry File to stop the command prompt from closing
After applying this method, it ensures that the command prompt will never close immediately after the batch script file is executed every time and the user need not edit the batch file for adding either pause or cmd /k.
We will show you how this can be done by changing some settings by editing the registry file using the registry editor as shown below.
Warning – Before continuing, make sure you export/save the backup of your registry file so that you can revert back to the original registry file if something goes wrong while editing.
Step 1: First you must open the registry editor. To do so, you need to press the Win + R keys together, which opens the Run command box.
Step 2: Type regedit in the Run box and press Enter key.
Step 3: Accept the User Access Control prompt by clicking Yes to continue.
Step 4: After the registry editor window opens, you should clear the address bar and copy-paste the below line and press Enter.
Step 5: Make sure you have selected the registry key called command and on the right side, double-click on the string value called (Default), which opens its edit window.
Step 6: Now type the below line in its Value Data textbox and click OK.
cmd.exe /k "%1" %*
Step 7: Now go to the address bar and clear it and then type the following path and press Enter key.
Step 8: Once you reach the command registry key, open the edit window of the (Default) string value by double-clicking on it.
Step 9: After that, enter the below line in the Value data field and tap on OK.
cmd.exe /k "%1" %*
Step 10: Once done, close the registry editor window.
Step 11: Now restart your system once and after the system starts, you can see that the command prompt will not close after any batch script file gets executed.
Hey! I am a software engineer who loves to solve technical issues and guide people in a simple and effective way as possible. Currently writing articles on tech problems is my thing!