12

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.

1
  • 2
    I can't test now, but Alt+space generally opens pop-up menu and then "c" closes the application. Dec 28, 2018 at 2:55

3 Answers 3

18

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.

3
  • 4
    This pointed me in the correct direction...but two things had to be done first: (1.) I had to set the execution policy of PowerShell using the following command (as admin): Set-ExecutionPolicy RemoteSigned (2.) I had to create a Profile before I could edit one, with the following command: New-Item -path $profile -type file –force Dec 29, 2018 at 2:53
  • 5
    If you want the Linux way to work (ctrl+d), make sure to use lower case "d": Set-PSReadlineKeyHandler -Chord Ctrl+d -Function DeleteCharOrExit Jul 21, 2020 at 18:39
  • Awesome, thanks a lot! Why doesn't this have like a hundred upvotes? :D This is most definitely the most annoying little thing if you work in Windows Terminal with some tabs running WSL and others running PS, and you instinctively press Ctrl+D to make your tabs close but all you get is ^D^D^D lol
    – bviktor
    Oct 31, 2022 at 23:38
4

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

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .