2

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
    I can't test now, but Alt+space generally opens pop-up menu and then "c" closes the application. – Máté Juhász Dec 28 '18 at 2:55
2

While Alt+Space, then c works as pointed out above without any changes, it still requires two key strokes. You can define your own exit shortcut in PowerShell 5.0 and up by addding 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. Open a PowerShell terminal window and type the following to edit your PowerShell Profile file:

    notepad $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 PS profile see this Microsoft reference page.

  1. 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 instead use this line:

Set-PSReadlineKeyHandler -Chord Ctrl+D -Function DeleteCharOrExit

Now close the PowerShell terminal and reopen. The shortcut keys defined above (Alt-F4 or Ctrl-D as applicable) should now work.

  • 1
    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 – Frank Fanelli Dec 29 '18 at 2:53
1

Try Alt+Space c meaning Alt and space together for the popup menu, then press c for Close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.