If you run a command in Command Prompt and the window slams shut before you can read the output, the fix is usually one of a few simple settings. This article explains why it happens and gives you seven reliable ways to keep the window open, from adding a single line to your command to changing a registry key.
Why the Command Prompt Window Closes
Command Prompt is a console host that runs a shell (usually cmd.exe) inside a window. When you type a command and press Enter, cmd.exe executes it and then waits for the next command. The window closes only when the shell process exits. That happens in two common scenarios:
- You double-click a .bat or .cmd file. The script runs, and when it finishes, the shell exits. If the script doesn’t pause, the window vanishes.
- You run a command from the Run dialog (Win+R) or from a shortcut. The command executes, and the console closes immediately after.
In contrast, when you open Command Prompt from the Start menu, the window stays open because the shell is waiting for your next command. So the problem is almost always about how you launched the command, not a broken Windows setting.
Method 1: Add pause to the Script
The most direct fix for batch files is to add the line pause at the end. This tells the script to wait for a key press before exiting. For example, a script that checks your IP address:
ipconfig pause
When you double-click it, the window stays open and shows “Press any key to continue . . .”. This works for any batch file and is the standard practice for scripts meant to be double-clicked. If you don’t want to edit the file, you can also run it from an already-open Command Prompt window—then the window won’t close because the parent shell is still running.
Method 2: Use cmd /k in Shortcuts and Run
If you’re launching a command from the Run dialog or a shortcut, prefix it with cmd /k. The /k switch tells cmd.exe to run the command and then keep the window open. For example, in Run (Win+R), type:
cmd /k ipconfig
For a shortcut, set the Target field to:
C:\Windows\System32\cmd.exe /k ipconfig
This is the cleanest way for one-off commands. The opposite switch, /c, runs the command and then closes—that’s the default when you use Run without cmd.
Method 3: Change the Registry to Keep the Window Open
If you want every double-clicked batch file to stay open automatically, you can change a registry key. Open regedit.exe and navigate to:
HKEY_CLASSES_ROOT\batfile\shell\open\command
The default value is usually "%1" %*. Change it to:
cmd /k "%1" %*
This makes double-clicking a .bat file run it with /k, so the window remains open. Be aware that this affects all batch files on your system. If you later want to revert, change the value back to "%1" %*. Always back up the registry before editing, and note that this does not affect .cmd files—you’d need to do the same for cmdfile\shell\open\command.
Method 4: Use a Helper Command Like timeout or cmd /k in the Script
Instead of pause, you can use timeout /t 5 to wait 5 seconds, which is useful if you want the script to continue after a delay. For example:
echo Done! timeout /t 5
The timeout command accepts a number of seconds (up to 9999) and can be interrupted with a key press. Another option is cmd /k at the end of the script, which would start a new nested command prompt—but that’s usually overkill. For most cases, pause or timeout is sufficient.
Method 5: Run the Command from an Already Open Prompt
The simplest workaround is to open Command Prompt first (from Start menu, or by typing cmd in Run and pressing Enter), then type your command. The window stays open because the shell is still running. This is the most natural approach for interactive use. If you’re testing a batch file, you can drag and drop it onto the open window, or type its full path. This method also lets you see any error messages that might scroll by too fast otherwise.
Method 6: Adjust the Shortcut’s “Close on Exit” Behavior
Some third-party console emulators (like ConEmu or Windows Terminal) have a setting that controls whether the tab closes when the shell exits. In Windows Terminal, go to Settings → Profile → Command line and add /k to the command line if you want it to stay open. For the classic console, there’s no such setting—the window always closes when the process exits. So if you’re using a custom terminal, check its documentation for “close on exit” options.
Method 7: Use PowerShell Instead
If you’re tired of this issue, consider using PowerShell. It has a similar -NoExit switch:
powershell -NoExit -Command "ipconfig"
Or, if you’re already in PowerShell, just run the command normally—the window stays open. PowerShell also supports pause and Read-Host for interactive waiting. The switch to PowerShell is not a direct fix, but it avoids the problem for many users who need a more powerful shell anyway.
Common Mistakes and Misconceptions
One common mistake is putting pause inside a for loop or after a command that exits the script (like exit). If your script has exit at the end, pause will never be reached. Another misconception is that changing the registry key for .bat files will affect all scripts—it does, but only for double-clicking, not when you run them from an open prompt. Also, note that cmd /k in a shortcut will keep the window open, but if your command itself closes the shell (e.g., exit), the window will still close. Finally, some users think the window closes because of an error—but in most cases it’s just the normal exit behavior.
Frequently Asked Questions
Why does my batch file close immediately even with pause?
If pause is not at the very end of the script, or if the script has a syntax error that causes it to exit before reaching pause, the window will close. Also, if you run the script from a shortcut that has “Close on exit” enabled (rare), it might override. Check the last line of your script and make sure there’s no exit before pause.
Can I make the window stay open only for certain commands?
Yes. Use cmd /k in the Run dialog or shortcut for that specific command. For batch files, you can add pause or timeout only to the scripts that need it. There’s no global setting that affects only some commands—it’s all or nothing.
Does cmd /k work in Windows 10 and 11?
Yes, cmd /k is a built-in switch of cmd.exe and works in all modern Windows versions, including Windows 10 and 11. It also works in the legacy console and in Windows Terminal.
What if I want the window to close after a delay, not on key press?
Use timeout /t 10 instead of pause. This waits 10 seconds and then exits. You can also use ping -n 11 127.0.0.1 > nul as a hacky delay, but timeout is cleaner and built-in.
Is it safe to edit the registry to keep the window open?
Editing the registry is generally safe if you know what you’re changing, but always back up the key first. The change we described only affects how batch files are launched when double-clicked. If you’re uncomfortable, stick to adding pause to your scripts—it’s simpler and reversible.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Custom Alphanumeric Passcode iPhone: How to Set
- HomePod Software Update: How to Install
- How to Shuffle Photos in Slideshow on iPhone
- Quick Look Not Working on Mac? Fixes That Work
- How to See Deleted WhatsApp Messages on iPhone
- Enable Handwriting Mode on iPhone & iPad in iOS 10