The command prompt, once launched, will only close when the user types the
exit command or click the close window button. But it also happens that the command prompt closes itself. That happens without showing the results of the completed script. This behavior of the console can be spotted when executing script files, such as CMD and BAT files.
This is by design and there is nothing wrong with the default behavior. But sometimes it is necessary to keep the console open after running the script from File Explorer. For example, to waits for the next command to be entered, or to read some output.
Well, changing the command prompt behavior is a very easy task. The easiest way is to add a pause command or a special argument to the end of the batch file that starts a new instance of the command prompt.
Open the batch file in Notepad or any other text editor, and one of the following commands to the end of the file:
- cmd /k
Save the file. These are two different methods and they work differently.
The pause command
The pause command interrupts the execution of the script and displays the message “Press any key to continue…”. If the command is added to the end of the script, pressing any key will close the console, but if pause is in the middle of the code, after pressing any key, the execution of the script will continue to the below lines.
Cmd /k command
When added to the end of the script, it starts a new console, displaying the standard prompt for a new command.
If you add cmd /k in the middle of the batch file, its execution will be interrupted and terminated.
This is the key difference between these commands.
If you don’t want to mess around with each batch file, you can change the default execution command for batch files in the registry.
How to prevent any batch file from closing by default
- Open the registry editor with the
- Expand the
- Change the default unnamed value in the right side from
cmd.exe /k "%1" %*.
- Now do the same under the
That’s all. From now on any batch file will end up with a command prompt when launched from the File Explorer. It will have to be closed only by the user, unless the exit command is explicitly included in the batch file.