What is a keyboard shortcut for closing a Windows PowerShell window?

I have opened an empty Windows PowerShell window, and have not yet entered any commands into the window. The default, unchanged directory is my user account folder. I want to close the window with a keyboard shortcut.


Does NOT work:

  • Ctrl-w (Adds the text ^W into the editor)

  • Ctrl-d (Adds the text ^D into the editor)

  • Alt-F4 (Does nothing)

  • exit followed by {Enter} (Works, but is a cumbersome sequence)


Is there any native solution to this problem without using an AutoHotkey script? I am using Windows 10.


Solution 1:

While Alt+Space then C works as pointed out above without any changes, it still requires two keystrokes. You can define your own exit shortcut in PowerShell 5.0 and up by adding a Set-PSReadLineKeyHandler command to your Powershell profile. While this does require editing your PS Profile, once set up it works for all future sessions of PowerShell. As an example,

  1. Set the execution policy of PowerShell as admin:

    Set-ExecutionPolicy RemoteSigned
    
  2. Open a PowerShell terminal window and type the following to edit your PowerShell Profile file:

    code $Profile
    

    This will open your Powershell profile. If you'd like this to work for all users, edit the AllUsers profile which is located at $PROFILE.AllUsersCurrentHost. For more information on the PowerShell profile see this Microsoft reference page.

  3. Add the following to the first line of the profile (ViExit is only available in PS 5.1):

    Set-PSReadlineKeyHandler -Chord Alt+F4 -Function ViExit
    

    This defines the Alt+F4 as the exit key command. If you'd like to use Ctrl+d (not D) instead use this line:

    Set-PSReadlineKeyHandler -Chord Ctrl+d -Function DeleteCharOrExit
    
  4. Now close the PowerShell terminal and reopen. The shortcut keys defined above (Alt+F4 or Ctrl+d as applicable) should now work.

Solution 2:

Try Alt+Space c meaning Alt and space together to open the shortcut menu for the active window, then press c for Close

enter image description here

ref: windows keyboard shortcuts

Solution 3:

Another option is to use AutoHotkey, the following script enables using Alt+F4 or Ctrl+D to close PowerShell:

#IfWinActive ahk_exe powershell.exe
  !F4::
  ^d::WinClose
#IfWinActive