3 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause)

When we run any batch file, the Command Prompt window will appear shortly and it goes on the same time and you do not exactly get what commands run on CMD as the result of the batch file.

The batch file contains commands to perform some actions related to System and these commands are executed one after another. So what if you want to watch your commands that you run through a batch file, and you want to prevent Command Prompt window from closing automatically.

Here are the 3 ways to prevent Command Prompt window from closing after running commands. Go for any solution you find easy and best for you.

First way

Open the batch file in Notepad by right clicking on it and click on “Edit” from the list.

3 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause) 1

And paste the following command at the end of your batch file

“cmd /k”

3 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause) 2

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.

3 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause) 3

This is the very easy and handy way to prevent the Command Prompt window from closing after running commands.

Second Way

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.

3 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause) 4

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.

3 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause) 5

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 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause) 6

Third Way

You can prevent Command Prompt window from closing after running commands through adding a Registry Key in the Windows Registry. For 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” \\\ “\””

After saving the file, double click on it. This registry will save to your Registry Editor of Windows. Now run any command, the Command Prompt window will stay open as long as you will keep it open.

3 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause) 7

These all are easy and quick ways to keep your window of the Command Prompt open so that you can easily configure and watch each command which you run through the batch file or Powershell Script. If you want to know anything else related to this, comment below. Thank you.

Read Next:

6 thoughts on “3 Ways To Prevent Command Prompt From Closing After Running Commands (Batch File Pause)”

  1. 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

    Reply
  2. 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.

    Reply
  3. 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

    Reply

Leave a Comment