Save yourself hours of time with this PowerShell PSReadline tweak

As someone who is absolutely obsessed with automation, I am always looking for little hacks or tweaks to make myself more efficient. Since I spend a lot of time in the PowerShell console, I thought I would share my favorite PSReadline setting and one that I HIGHLY recommend to anyone who wants to save themselves hours if not days of their life trying to remember exactly what command they used last week to do task X. With that said, I am not crazy about throwing in a lot of stuff into my profile, but for me this is a must have.

In my PowerShell profile I have two lines that modify my console experience:

Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

So what do these two little beauties do exactly? They create and destroy worlds. Just kidding. They do save human lives though. Just kidding again. OK, enough with the suspense…

Basically, they allow the user to cycle back and forth through command history using the up and down arrow keys but with an added twist. In addition to cycling through your command history, it will autocomplete the rest of a command you start typing. So in this example I have typed Get-Process. When I hit the up arrow after Get-Process, the console will autocomplete with the last command I used in my history that started with Get-Process. At this point I can hit the up and down arrow which allows me to cycle through previous instances.

Update: I previously stated I hit tab to autocomplete the rest of the command in Get-Process. I meant I hit up arrow not tab.

Awesome right? Keep in mind the command history is not dependent on the current session so it includes other previous sessions as well. Enjoy!

4 Responses to “Save yourself hours of time with this PowerShell PSReadline tweak

  • Great tip 🙂

  • Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
    generating error A Paremeter cannot be found that matches parameter name ‘PSReadlineKeyHandler’

    • Dan Franciscus
      7 years ago

      I think you have a space between Set and -PSReadlineKeyHandler. Should be no spaces. Set-PSReadlineKeyHandler.