When we execute a batch file, the Command Prompt opens and closes automatically after processing the batch file. Sometimes, the command is executed so quickly that it seems like the Command Prompt closes immediately after opening the batch (BAT) file. This prevents users from performing other tasks after the execution of the batch file or viewing the status of the commands they executed in the CMD. If you want the Command prompt window to remain open after processing the batch file, this post may help you.
Command prompt closes immediately after opening batch file
You can prevent Command Prompt from closing immediately after opening a batch file in the following three ways:
- By using the Pause command.
- By using the cmd /k command.
- By editing the Registry keys.
Let’s see these methods one by one.
1] Prevent Command Prompt from closing immediately by using the Pause command
The Pause command prevents the Command Prompt from closing after processing the batch file.
Follow the steps below:
- Right-click on the batch file and select Edit. This will open the Notepad.
- Type Pause at the end of the command that you have typed in the batch file.
- Save the file.
Now, when you open the batch file, the Command Prompt will not close automatically after processing the file. Instead, it will display a message “Press any key to continue…” When you press any key, the Command Prompt will close.
The Pause command will only let you see how the commands are executed. If you want the Command Prompt to remain open so that you can execute other commands on the same window, you have to use the next method.
2] Prevent Command Prompt from closing by using the cmd /k command
cmd /k command lets you execute new commands after the processing of the batch file without closing the Command Prompt. Right-click on the batch file and type
cmd /k at the end of the batch file. After that, save it.
3] Editing the Registry key to stop Command Prompt from closing
The above two methods stop Command Prompt from closing immediately after processing the batch file. But the only downside of these two methods is you have to use those commands in every batch file. If you already have many batch files, you have to edit all of them.
You can skip editing the batch files by modifying the Registry keys. We have explained the procedure below. Before you proceed, we recommend you create a backup of Registry.
1] Launch the Run command box. Type
regedit and click OK. Click Yes if you get a UAC prompt.
2] In the Registry Editor, navigate to the following paths:
You will find a “(Default)” value on the right side. Double click on that value and change its Value data to
cmd.exe /k "%1" %*. Click OK to save the changes you made. Close the Registry Editor when you are done.
If after trying the above three methods, the Command prompt is still closing automatically on your system, we suggest you try the following fixes:
- Run SFC scan.
- Troubleshoot in a clean boot state.
- Reset your PC.
1] Run SFC scan
The System File Checker (SFC) is a utility from Microsoft that scans the system files for errors or corruptions and repairs the corrupted files. The issue that you are experiencing may be due to corrupted system files. Hence, running an SFC scan may fix the problem.
2] Troubleshoot in a clean boot state
Sometimes, a faulty program creates several issues on a Windows PC. You can identify the faulty program or software by troubleshooting in a clean boot state. If you find a program the culprit of the problem, consider uninstalling it.
3] Reset your PC
If nothing helps you fix the error, consider resetting your PC.
Hope this helps.
How do I run an EXE from Command Prompt?
To run an EXE from Command Prompt, follow the steps below:
1] Launch the Command Prompt.
cd "file path" without quotations and hit Enter. The file path is the installation location of the EXE file. To know the file location, right-click on the shortcut icon of the program that you want to open via CMD and select Open file location. Now, click on the address bar of the File Explorer and copy the entire path. You can paste this entire path directly in the Command Prompt by using the Ctrl + V keys.
3] Now, type
start "filename.exe" without quotations and press Enter.
This will launch the EXE file.
How do I keep a command window open in a batch file?
You can use either Pause or cmd /k commands to keep the command window open in a batch file. If you want, you can also make this action permanent by modifying some keys in the Registry. We have explained all these three methods above in this article.