How to Stop Command Prompt from Closing Immediately After a Command
If Command Prompt opens, runs a command, and closes immediately, the command was probably started from Run, a shortcut, a script, or a batch file that ends automatically. The simplest fix is to open cmd.exe first and run the command inside the already-open window.
cmd, press Enter, and then run your command in the Command Prompt window. The window will usually stay open after the command finishes.
Open CMD first
Start Command Prompt manually, then paste or type the command. This lets you read errors and output.
RecommendedUse cmd /k
The /k switch runs a command and keeps the window open after it finishes.
Add pause
The pause command waits for a key press so you can read the last message before closing.
Why CMD Closes After Running a Command in Windows 10 and Windows 11
This behavior is often normal. A console window closes when the process that opened it finishes. For example, if you double-click a .bat file or run a command through the Run dialog, Windows may create a temporary console window only for that command. When the command ends, the temporary window closes.
| Situation | Why it closes | Best fix |
|---|---|---|
| You run a command from Run | Windows starts a temporary cmd.exe process |
Open CMD first or use cmd /k |
You double-click a .bat or .cmd file |
The script ends and the console window exits | Add pause at the end while testing |
| A shortcut starts a command | The shortcut target may use /c |
Change /c to /k |
| A script fails immediately | There may be a syntax error, missing file, or permission problem | Run it from an open terminal and read the error |
Open Command Prompt First, Then Run the Command
This is the safest troubleshooting method because the window remains open and you can see the complete output.
- Press Win+R.
- Type
cmdand press Enter. - Run the command inside the already-open Command Prompt window.
- Read the output, error message, or exit status before closing the window manually.
For commands that require administrator rights, open Start, type Command Prompt, choose Run as administrator, and then run the command.
Use cmd /k to Keep the Command Prompt Window Open
The cmd command supports switches that control what happens after a command runs. The two most common switches are /c and /k.
Use /k when you want to keep CMD open
- Runs the command.
- Leaves the console window open.
- Lets you read the output and continue typing.
Use /c when you want CMD to close
- Runs the command.
- Closes the console after the command finishes.
- Useful for automation, but bad for reading errors.
Example: this opens Command Prompt, runs ipconfig, and keeps the window open:
Commandcmd /k ipconfig
Example: this runs a command and closes the window when it finishes:
Commandcmd /c ipconfig
cmd /c. Replacing it with cmd /k is often enough during troubleshooting.
Fix a BAT or CMD File That Closes Immediately After Running
If a .bat or .cmd file closes immediately, add pause at the end while testing. This makes the script wait until you press a key.
Batch file example@echo off
ipconfig /all
pause
If the script has several branches, add pause before each possible exit line or before a section where you suspect the script fails.
Debugging example@echo off
echo Starting script...
net use
if errorlevel 1 (
echo The command failed.
pause
exit /b 1
)
echo Done.
pause
Run the batch file from an existing CMD window
Another good method is to open Command Prompt, go to the folder where the batch file is located, and run it manually.
Commandcd /d "C:\Path\To\Folder"
my-script.bat
When launched this way, the script can end, but the parent cmd.exe window usually remains open.
Check Shortcut Targets That Make Command Prompt Close
If CMD closes after opening a desktop shortcut, the shortcut target may be configured to close the console after the command completes.
- Right-click the shortcut and select Properties.
- Open the Shortcut tab.
- Check the Target field.
- If it contains
cmd /c, change it tocmd /kwhile testing. - Click Apply, then run the shortcut again.
For example, change this closing version:
Shortcut target that closesC:\Windows\System32\cmd.exe /c ipconfig
To this version that stays open:
Shortcut target that stays openC:\Windows\System32\cmd.exe /k ipconfig
PowerShell, Python, and Installer Commands Can Also Close Too Fast
The same problem can happen with PowerShell, Windows Terminal, Python, installers, and command-line utilities. If you double-click a script file, Windows may open a console only for that script and close it when the process exits.
Run from an open PowerShell window
Open PowerShell first, then run the script so the error remains visible.
Good for .ps1Run from CMD or Terminal
Open Command Prompt, go to the script folder, and run python script.py.
Use logging options if available
Some installers write logs even if the console closes. Check the installer documentation or temporary folders.
For setup toolsFor PowerShell scripts, you can also add a temporary read prompt at the end while debugging:
PowerShell debug lineRead-Host "Press Enter to exit"
How to Find the Error Before the CMD Window Disappears
If the window closes before you can read the text, the goal is to capture the output. You can keep the window open, redirect output to a file, or check system logs depending on the command.
Redirect command output to a text file
This saves both normal output and error output to a file on the desktop:
Commandyour-command-here > "%USERPROFILE%\Desktop\cmd-output.txt" 2>&1
Replace your-command-here with the real command. Then open cmd-output.txt on the desktop and read the result.
Show each command in a batch file
If your batch file starts with @echo off, temporarily remove it or change it to echo on. This helps you see which line runs before the script exits.
Batch debug modeecho on
rem Your commands go here
pause
Check common mistakes
- The command path is incorrect or the file no longer exists.
- The command needs administrator rights.
- The script uses
exittoo early. - The shortcut uses
/cinstead of/k. - A required tool is not installed or is not in the
PATHenvironment variable. - Quotation marks are missing around a path with spaces.
When CMD Opens and Closes by Itself: Check Startup Tasks and Malware
If Command Prompt opens and closes by itself without you running anything, treat it differently. It may be a scheduled task, a startup entry, a driver utility, a software updater, or unwanted software.
- Open Task Manager and check the Startup apps list.
- Open Task Scheduler and review recently added or suspicious tasks.
- Run a full scan with Windows Security.
- Check whether a specific application opens CMD during updates or maintenance.
- If the window appears repeatedly, use Reliability Monitor or Event Viewer to look for related errors at the same time.
Command Prompt Closes After Running a Command: Frequently Asked Questions
Q Why does Command Prompt close after I double-click a BAT file? โ
pause at the end while testing, or run the batch file from an already-open Command Prompt window.
Q What is the difference between cmd /c and cmd /k? โ
cmd /c runs a command and then closes. cmd /k runs a command and keeps the console open. Use /k when you need to read the result.
Q Is it a virus if CMD opens and closes quickly? โ
cmd.exe. However, if it happens repeatedly, at startup, or together with browser redirects, blocked security tools, or unknown processes, run a security scan and inspect startup tasks.
Q How do I keep a Python script window open in Windows? โ
python script.py. You can also add a temporary input line in the script while debugging, but running it from an open terminal is usually cleaner.
Q Should I add pause to every batch file? โ
pause while testing or when the file is intended for manual use. Remove it for unattended automation, scheduled tasks, logon scripts, and deployment scripts where waiting for a key press would break the workflow.
Best Fix When Command Prompt Closes Too Quickly
For most cases, the fix is simple: open Command Prompt first and run the command inside it. If you are editing a shortcut, use cmd /k instead of cmd /c. If you are debugging a batch file, add pause near the end or before the line where the script exits.
๐ฏ Quick Decision Guide
Need to read output: open cmd.exe first and run the command manually.
Shortcut closes too fast: replace cmd /c with cmd /k.
Batch file closes instantly: add pause while testing.
Window appears by itself: check startup apps, scheduled tasks, and security scan results.
The key is to separate normal console behavior from real errors. Once the window stays open, the error message usually tells you exactly what needs to be fixed.