The PowerShell exit command is your fastest way to terminate a script, command prompt, or interactive session without closing the entire application—often with just one keystroke or two. It’s essential for quick exits, debugging, or when you’ve hit an error. Unlike the traditional exit command, PowerShell’s exit variants include shortcuts like exit -Force, exit 0, or even q (in extended mode). This guide covers all methods, best practices, and common pitfalls.
What You'll Need
- PowerShell installed on Windows (comes pre-installed in modern Windows versions).
- Access to a terminal window (usually opened via
powershellin CMD or via the Start menu). - A script or active command session (optional; the exit command works in both).
Step-by-Step
-
Use the default exit command. Open PowerShell and type:
exit. This exits the current session and returns you to the command prompt. The exit code is0by default, indicating success. If that did not work: Restart PowerShell or check for errors in the session. -
Use
exit -Force. To bypass prompts (e.g., saving changes), add-Force:exit -Force. This forces immediate termination without asking for confirmation. If that did not work: Ensure no hidden scripts or processes are blocking the command. -
Exit with a custom error code. For scripts, exit with a non-zero code (e.g.,
1) to indicate failure:exit 1. Useful for logging or conditional logic in scripts. If that did not work: Verify the script isn’t running in an infinite loop or stuck in a background task. -
Use the
qshortcut (extended mode). If you’re in an interactive session, typeqin **extended mode** (enabled withSet-PSReadLineOption -ConfirmSwitch $falsebefore commands). This is faster thanexitfor quick exits. If that did not work: Ensure extended mode isn’t disabled by another command. -
Exit to CMD from PowerShell. To return to Command Prompt (CMD) without closing PowerShell entirely, use:
Set-PSHost -ExitProcess. This resets PowerShell’s host and opens a new CMD session. If that did not work: Close and reopen PowerShell; some environment variables may persist.
Common Mistakes
- Misusing exit codes. Forgetting that
exit 0means success whileexit 1means failure can confuse scripts. Always check the exit code if debugging. - Ignoring confirmation prompts. PowerShell may ask for confirmation before exiting if changes are pending (e.g., saving a script). Use
-Forceto bypass it. - Running in an elevated session incorrectly. If PowerShell was opened as admin, closing it may terminate admin privileges. Use
exitwithout admin rights or restart the session instead. - Using the wrong mode. The
qshortcut only works in extended mode. Enable it first if needed.
Tips
- Shortcuts for frequent exits. Bind
Ctrl+Z+Enterto kill a stuck command in extended mode. PressCtrl+Cto abort running commands. - Debugging scripts. Redirect output to a file before exiting to avoid losing data:
exit 1 || Out-File -FilePath "log.txt". - Terminate long-running tasks. Use
Stop-Processto end active processes before exiting:Stop-Process -Name "notepad". - Check exit status. After exiting a script, inspect the exit code to verify success/failure:
$?; exit $LASTEXITCODE.
Troubleshooting
-
PowerShell won’t exit. The session may be frozen or stuck. Try:
- Press
Ctrl+Alt+Deland select Task Manager > End Task. - Restart PowerShell.
- Check for locked files or processes with:
Get-Process | Where-Object {$_.Id -like "*powershell*"} | Stop-Process.
- Press
- The exit command isn’t recognized. PowerShell may be in a different mode (e.g., Jupyter notebook or WSL). Close and reopen the terminal or switch to a regular PowerShell session.
-
Admin rights lost after exit. If PowerShell was opened as admin, closing it may reset your user context. Use
exitwithout admin rights or restart the session. -
Background tasks persist. Some commands (e.g.,
Start-SleeporInvoke-WebRequest) run in the background. Kill them with:Stop-Process -Id, whereis found via Get-Process | Select-Object Id, Name.
Frequently Asked Questions
Can I exit PowerShell without closing the terminal?
Yes, but the behavior depends on the mode. Use exit for a new terminal session, or Set-PSHost -ExitProcess to return to CMD without restarting PowerShell.
Why does PowerShell ask for confirmation before exiting?
PowerShell may prompt for changes to pending commands (e.g., unsaved script blocks) or elevate privileges. Use -Force to bypass it.
Is there a one-key way to exit PowerShell?
The fastest method is Ctrl+Z + Enter (extended mode) or q (extended mode). Enable extended mode first with Set-PSReadLineOption -ConfirmSwitch $false.
How do I exit a script with a specific error code?
Use exit $errorCode, where $errorCode is a number (e.g., 1). This ensures scripts can check the exit status later.
Can I exit PowerShell from a remote session?
Yes, but remote sessions (e.g., via RDP or SSH) may require additional steps. Use exit as normal, but check for terminated sessions with Get-PSSession first.
Why does my exit command fail in a WSL session?
WSL (Windows Subsystem for Linux) uses a different shell. Exit with exit or Ctrl+D in WSL, or switch to PowerShell’s native exit commands.