Keyboard Shortcut for Quitting Powershell Keyboard Shortcut for Quitting Powershell powershell powershell

Keyboard Shortcut for Quitting Powershell


To close a PowerShell window using the system menu you can use

Alt + Spacebar, C

Nearly every Windows window has this option. The Close or Exit option may differ based on the context so you'll just have try it and see.

This answer is accurate for the time of its writing but things may change.


Update: If you're using Windows Terminal, no workaround is needed: Alt-F4 works as-is.
The following therefore only applies to regular (conhost.exe) console windows.


Note:

  • For an ad hoc solution that uses different keys (strictly speaking, a keyboard shortcut followed by an additional, UI-language-dependent keypress), but requires no setup, see No Refunds No Returns's answer.

  • This answer provides a workaround that works transparently, i.e., with the original shortcut key, but it requires modification of your $PROFILE file; on the plus side, you can use the same technique to make other unavailable shortcuts available too.


It is the imported-by-default PSReadline module that prevents Alt+F4 from being effective[1].

While unloading PSReadline (Remove-Module -Force PSReadline) is an option in principle, you'll lose valuable command-line editing features if you do.

The best approach is therefore to define a Alt+F4 handler for PSReadline that emulates the default behavior:

Add the following to your $PROFILE, which sets up a PSReadline key-chord handler that uses a WSH COM object to send Alt+F4 to the caller's console window, which closes it:

Set-PSReadlineKeyHandler Alt+F4 -ScriptBlock {   (New-Object -ComObject WScript.Shell).SendKeys('%{F4}')}

[1] As of v2.0.0-beta3. The longstanding underlying technical problem is explained in this GitHub issue, which also contains workarounds for other standard keyboard shortcuts, such as Ctrl+F.