Windows Troubleshooting ยท 2026

Command Prompt Closes After Running a Command in Windows: How to Fix It

A practical guide to keeping CMD open, fixing batch files that close instantly, checking shortcut targets, and finding the real error message before the window disappears.

โŠž Windows 10 โŠž Windows 11 ๐Ÿ’ป Command Prompt ๐Ÿ“„ Batch Files ๐Ÿ• 7 min read

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.

๐Ÿ’ก
Fast fix Press Win+R, type cmd, press Enter, and then run your command in the Command Prompt window. The window will usually stay open after the command finishes.
Best for users

Open CMD first

Start Command Prompt manually, then paste or type the command. This lets you read errors and output.

Recommended
Best for shortcuts

Use cmd /k

The /k switch runs a command and keeps the window open after it finishes.

Shortcut fix
Best for batch files

Add pause

The pause command waits for a key press so you can read the last message before closing.

Script fix

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
โš ๏ธ
Important A closing window does not always mean the command failed. Some commands finish correctly and simply do not wait for you to read the result.

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.

  1. Press Win+R.
  2. Type cmd and press Enter.
  3. Run the command inside the already-open Command Prompt window.
  4. 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.

โœ…
Recommended Use this method when you are running repair commands, disk commands, network reset commands, or anything that may show an error you need to copy.

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
๐Ÿ”Ž
Rule If a shortcut or script closes too quickly, look for 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.

  1. Right-click the shortcut and select Properties.
  2. Open the Shortcut tab.
  3. Check the Target field.
  4. If it contains cmd /c, change it to cmd /k while testing.
  5. 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
๐Ÿงญ
Shortcut note Keep quotation marks around paths that contain spaces. A broken target path can make a script fail instantly, which looks similar to CMD closing too fast.

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.

PowerShell

Run from an open PowerShell window

Open PowerShell first, then run the script so the error remains visible.

Good for .ps1
Python

Run from CMD or Terminal

Open Command Prompt, go to the script folder, and run python script.py.

Good for .py
Installers

Use logging options if available

Some installers write logs even if the console closes. Check the installer documentation or temporary folders.

For setup tools

For 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

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.

  1. Open Task Manager and check the Startup apps list.
  2. Open Task Scheduler and review recently added or suspicious tasks.
  3. Run a full scan with Windows Security.
  4. Check whether a specific application opens CMD during updates or maintenance.
  5. If the window appears repeatedly, use Reliability Monitor or Event Viewer to look for related errors at the same time.
๐Ÿ›ก๏ธ
Security warning Do not approve unknown administrator prompts or run unknown commands copied from websites. If CMD appears repeatedly without a clear reason, scan the system and review startup items before making registry changes.

Command Prompt Closes After Running a Command: Frequently Asked Questions

Q Why does Command Prompt close after I double-click a BAT file? โŒ„
Because Windows opens a console for the batch file, runs the script, and closes the console when the script ends. Add 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? โŒ„
Not always. Many legitimate tasks, update tools, and scripts use 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? โŒ„
Open Command Prompt first, go to the script folder, and run 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? โŒ„
No. Add 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.