CMD Simulator
System Informationexit

EXIT Command – Exit Command Prompt or Batch Script in Windows

Learn how to use the EXIT command to close the Command Prompt window or exit a batch script in Windows. Syntax, /B option, exit codes, and when to use EXIT vs closing the window.

Rojan Acharya··Updated Mar 15, 2026
Share

The EXIT command is a Windows Command Prompt utility that exits the CMD.EXE program (command interpreter) or, when used with /B, exits only the current batch script and returns control to the caller. Use EXIT to close the Command Prompt window or end a script with an optional exit code for error handling.

Whether you're writing batch files that need to signal success or failure, closing a remote or automated CMD session, or simply ending a terminal window from the keyboard, EXIT is the standard way to terminate the session or script cleanly.

This guide covers EXIT syntax, the /B option, exit codes, examples, and frequently asked questions.

What Is the EXIT Command?

EXIT has been part of Windows and MS-DOS for decades. Without options, it closes the current Command Prompt window (or the CMD session). With /B, it exits only the current batch file and returns an optional exit code (number) to the caller—useful for scripts that are invoked by other scripts or task schedulers.

EXIT runs in Command Prompt (CMD) and is available in all Windows versions. In a normal interactive CMD window, EXIT closes the window. In a batch file, EXIT (without /B) still closes the CMD window that is running the batch; EXIT /B only ends the batch file and keeps the window open if it was started interactively.

EXIT Command Syntax

EXIT [/B] [exitCode]

Parameters

ParameterDescription
/BExit only the current batch script; do not close the CMD window. If omitted, CMD.EXE exits (window closes when it was the main process).
exitCodeOptional numeric value (e.g. 0 for success, non-zero for error). Used with /B to return a code to the caller. Without /B, the code sets the process exit code when CMD exits.

Examples

Close the Command Prompt window

EXIT

In an interactive CMD window, this closes the window. In a batch file run by double-click, this also closes the window when the batch finishes.

Exit a batch script but keep the window open (/B)

@ECHO OFF
ECHO Doing work...
CALL other.bat
EXIT /B %ERRORLEVEL%

EXIT /B ends the current batch file and returns the exit code. The CMD window stays open if you started the batch from an already-open CMD. If you started the batch by double-clicking, the window will close when the top-level script exits unless you add a PAUSE or similar.

Exit with a success code

EXIT /B 0

Common convention: 0 = success. Callers (e.g. another batch, Task Scheduler) can check %ERRORLEVEL% after CALL.

Exit with an error code

EXIT /B 1

Non-zero typically indicates failure. Use consistent codes (e.g. 1 = general error, 2 = file not found) in your scripts.

Use in a script that checks results

@ECHO OFF
COPY file.txt backup\file.txt
IF %ERRORLEVEL% NEQ 0 (
  ECHO Copy failed.
  EXIT /B 1
)
EXIT /B 0

The script exits with 1 on copy failure and 0 on success, so a parent script or scheduler can react to the result.

Common Use Cases

  1. Closing CMD – Type EXIT when you are done to close the window.
  2. Batch scripts – Use EXIT /B with a code so callers or Task Scheduler can detect success/failure.
  3. Automation – Scripts that run from Task Scheduler or other tools often end with EXIT /B and a meaningful code.
  4. Chained scripts – Use EXIT /B to stop the current batch and pass an exit code back to the caller.

Tips and Best Practices

  1. Use 0 for success – Convention is 0 = success, non-zero = error. Many tools and schedulers rely on this.
  2. EXIT vs EXIT /B – Use EXIT when you want to close the CMD session; use EXIT /B when you only want to leave the current batch and possibly return a code.
  3. ERRORLEVEL – After CALL script.bat, %ERRORLEVEL% is set to the value passed to EXIT /B (or the last command’s exit code). Check it immediately after CALL.
  4. Double-clicked batches – A batch started by double-click runs in a new CMD; when the batch ends (with or without EXIT), that window can close. Use PAUSE at the end if you want to keep the window open to read output.

Troubleshooting

Window closes before I can read output

Add PAUSE at the end of the batch so the window waits for a key press. Or run the batch from an already-open CMD so the window does not close when the script exits.

Exit code not seen by caller

Ensure you use EXIT /B with a number (e.g. EXIT /B 1). The caller should check %ERRORLEVEL% right after CALL. Note that some commands set ERRORLEVEL; the last run command or EXIT /B wins.

Related Commands

  • CALL – Run another batch and return; use with EXIT /B so the called batch can pass back an exit code.
  • PAUSE – Pause and wait for a key press; use before EXIT if you want to read output before the window closes.
  • CLS – Clear the screen; use when you want to tidy the display, not end the session.

Frequently Asked Questions

What does EXIT do?

EXIT closes the current CMD.EXE session (and thus the Command Prompt window when it’s the main process), or with /B it only exits the current batch script and can set an exit code.

What is EXIT /B?

EXIT /B exits only the current batch file and optionally sets the exit code (e.g. EXIT /B 1). The CMD window can stay open if the batch was started from an existing CMD window.

What exit code should I use?

Use 0 for success and a non-zero value (e.g. 1, 2) for errors. Many automation tools and schedulers use the exit code to decide whether the task succeeded.

Can I use EXIT in PowerShell?

PowerShell has its own Exit keyword (and exit). It exits the current PowerShell process or script. For CMD-style behavior inside PowerShell, run cmd /c exit 1 if you need to run CMD and get an exit code.

Quick Reference Card

CommandPurposeExample
EXITClose CMD window / end sessionEXIT
EXIT /BExit current batch onlyEXIT /B
EXIT /B 0Exit batch with success codeEXIT /B 0
EXIT /B 1Exit batch with error codeEXIT /B 1

Summary

EXIT ends the Command Prompt session (closing the window) or, with /B, ends only the current batch script and optionally returns an exit code. Use 0 for success and non-zero for errors so callers and schedulers can detect failure. Use CLS to clear the screen and PAUSE to keep the window open to read output.

Try the simulator in your browser—sessions don’t actually close; use EXIT to see the message. For more commands see the Commands Reference and the Windows Command Simulator.