Whenever we run any batch file, the Command Prompt window will appear shortly, and it goes on simultaneously. You do not see what commands were running on the command prompt due to the batch file.
In the batch file, there are commands that perform actions related to the system, and these commands are executed sequentially. So what if you want to monitor your batch files and prevent Command Prompt from closing automatically if you run them?
Table of Contents
The following are three ways to prevent the Command Prompt window from closing after running commands. Choose the option that works best for you.
#1 Prevent CMD window from closing using cmd /k command switch
Open the batch file in Notepad by right clicking on it and click on “Edit” from the list.
Then paste the following command at the end of your batch file
cmd /k
Next, save the file by pressing “Ctrl+S” and close the notepad. Double click your .bat file. You will see that CMD will remain open as long as you want it to stay open.
This is a very easy and handy way to prevent the Command Prompt window from closing after running commands.
#2 Stop CMD window from closing automatically using PAUSE command
The second way I am going to show you is also a simple method.
Edit your bat file by right clicking on it and select “Edit” from the list. Your file will open in notepad.
Now add “PAUSE” word at the end of your bat file. This will keep the Command Prompt window open until you do not press any key.
You can easily see each command which had run on the result of bat file in CMD as long as you do not press any key.
#3 Permanently stop all cmd windows from closing using Windows Registry
You can prevent Command Prompt window from closing after running commands through adding a Registry Key to the Windows Registry. To do this, type the following code in your Notepad and save the file with the .reg extension.
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command] @=”\ ”C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\” –noExit \ “& \\\ “%1\\\ ”\”” [HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command] @=”\ “C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\ “ –NoExit \ “-Command\” \”if ( ( Get-ExecutionPolicy ) –ne ‘AllSigned’) { Set-ExecutionPolicy –Scope Process Bypass }; & \\\ ”%1” \\\ “\””
Double-click the saved file after it has been saved. Windows will save the registry values to the Registry. Run any command or batch script in CMD now, and the Command Prompt window will remain open as long as you keep it open.
These are all quick and easy ways to keep the Command Prompt window open, so you can configure and monitor each command executed through a batch file or Powershell script. If you have any other questions, please comment below. Many thanks.
7 comments
Travis
This was wildly helpful, thank you so much.
Gogu
Hello, can you please post the full version for that .reg file with the cmd.exe /k? The third way doesn’t work for me, and i really need that cmd to stay open after running basics programs with visual studio. Thanks in advace
Leonard
The third way for keeping a batch file window open assumes that you have Power Shell included in your Windows installation. Here is a possible solution for the standard Windows command interpreter:
Open regedit and go to each one of:
[HKEY_CLASSES_ROOT\batfile\shell\open\command]
[HKEY_CLASSES_ROOT\cmdfile\shell\open\command]
Now change the “Default key value” to cmd.exe /k “%1” %*. Now, every batch script window will stay open after its execution.
Michal
Hello,
I need to find a way to prevent my command prompt from closing but I can’t edit the batch file. It’s somewhere in my computer and automatically executes after every 5 minutes. It’s probably a virus and I’ve tried numerous programs and antiviruses to remove it but nothing works. The command prompt closes before I can read anything and preventing it from closing could help find the source. I’ve tried the third way but it doesn’t work for me. Are there any other ways how to prevent the prompt from closing?
Thank you
Usman Khurshid
Hi Michal, you should look for the automatically executing command prompt entry in your startup items.
mandala suresh
thank you
Ron
Fourth way:
Open a cmd window, type the file name, press enter.
The window will stay open until you close it.