Scott Hanselman

F7 is the greatest PowerShell hotkey that no one uses any more. We must fix this.

March 26, 2019 Comment on this post [14] Posted in Musings | PowerShell
Sponsored By

Thousands of years ago your ancestors, and myself, were using DOS (or CMD) pressing F7 to get this amazing little ASCII box to pop up to pick commands they'd typed before.

Holy crap it's a little ASCII box

When I find myself in cmd.exe I use F7 a lot. Yes, I also speak *nix and Yes, Ctrl-R is amazing and lovely and you're awesome for knowing it and Yes, it works in PowerShell.

Ctrl-R for history works in PowerShell

Here's the tragedy. Ctrl-R for a reverse command search works in PowerShell because of a module called PSReadLine. PSReadLine is basically a part of PowerShell now and does dozens of countless little command line editing improvements. It also - not sure why and I'm still learning - unknowingly blocks the glorious F7 hotkey.

If you remove PSReadLine (you can do this safely, it'll just apply to the current session)

Remove-Module -Name PSReadLine

Why, then you get F7 history with a magical ASCII box back in PowerShell. And as we all know, 4k 3D VR be damned, impress me with ASCII if you want a developer's heart.

There is a StackOverflow Answer with a little PowerShell snippet that will popup - wait for it - a graphical list with your command history by calling

Set-PSReadlineKeyHandler -Key F7

And basically rebinding the PSReadlineKeyHandler for F7. PSReadline is brilliant, but I what I really want to do is to tell it to "chill" on F7. I don't want to bind or unbind F7 (it's not bound by default) I just want it passed through.

Until that day, I, and you, can just press Ctrl-R for our reverse history search, or get this sad shadow of an ASCII box by pressing "h." Yes, h is already aliased on your machine to Get-History.

PS C:\Users\scott> h

  Id CommandLine
   -- -----------
    1 dir
    2 Remove-Module -Name PSReadLine

Then you can even type "r 1" to "invoke-history" on item 1.

But I will still mourn my lovely ASCII (High ASCII? ANSI? VT100?) history box.


Sponsor: Manage GitHub Pull Requests right from the IDE with the latest JetBrains Rider. An integrated performance profiler on Windows comes to the rescue as well.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
March 28, 2019 7:55
And yet the MSBuild script for my project still fails in Powershell but works flawlessly in a command prompt.
March 28, 2019 9:44
Set-PSReadLineKeyHandler : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Set-PSReadlineKeyHandler -Key F7
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-PSReadLineKeyHandler], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.SetPSReadLineKeyHandlerCommand
March 28, 2019 11:55
PowerShell has the Get-History and Invoke-History cmdlets for doing that and they also have alias (ghy or history for Get-History and ihy or just r for Invoke-History

Did you also know that PSReadline logs all your commands to disk by default? Have a look at it:
notepad (Get-PSReadLineOption).HistorySavePath
After this you'll think twice before copy-pasting plain-text secrets into the console...
March 28, 2019 13:07
@Fleet Command

Try running the script located at the URL mentioned in his article:

https://stackoverflow.com/questions/50376858/making-the-command-history-pop-up-work-via-f7-in-windows-10-powershell/50382377#50382377

I got the same error as you, but after I executed the script, I got the history window.
March 28, 2019 13:42
You can also start a command line with #, type a substring of the command in history you want to invoke, and hit tab to select the full command. Keep hitting tab if there are multiple matches.
March 28, 2019 13:44
Not a "graphical" approach, but IMHO far better: type '#xyz' where xyz is some part of the command in history you want to execute (for example '#get-childitem' or '#gci') then hit tab. This is a form of tab completion that completes the entire line based on your history rather than completing a parameter based on valid values. Like all tab completions you can tab multiple times to "scroll through" the available items (past commands) and even shift-tab (multiple times) to "scroll forward". Yes, not a nice popup which I'm sure is what you were after, since the up/down arrows already do what that popup does sans the popup, but this is far faster and more powerful than either of those, popup or no.
March 28, 2019 14:09
What I really want (in windows cmd or powershell) is the equivalent of Fuzzy Finder (I replaced the standard ctrl+r on bash)

https://github.com/junegunn/fzf

It's like reverse find but better
March 28, 2019 18:19
@Peter in these cases it is almost always something about the arguments to the native exe that PowerShell interprets. For instance `;` is a statement separator in PowerShell, `$` starts a variable, `@` a hashtable, etc.

You can usually get around these issues by using the `--%` (stop parsing) operator - which essentially dumbs down the parsing until EOL. Try this:

msbuild --% <rest of the msbuild args>
March 29, 2019 7:25
I've been working in this industry professionally for over twelve years and hobbying for fifteen, and this is the first time I've ever heard of using F7 in a CMD prompt to show the history like this.

Wow. Really, wow.
March 29, 2019 19:59
@Adam Straughan: fzf works under Windows. You can install it using Chocolatey (https://chocolatey.org/packages/fzf). There's a PowerShell wrapper that binds Ctrl+R, Ctrl+T, and a couple of other shortcuts that might be useful. It's available here: https://www.powershellgallery.com/packages/PSFzf/1.1.22.

(Full disclosure - I wrote the wrapper.)
March 31, 2019 1:43
...I just wish "r" (invoke-history), et al, worked like the ! in *nix-land, where you can give it a negative number to run the "nth-ago" command.
March 31, 2019 10:37
I've been using powershell for a couple of years and never knew about CTRL-r. Thanks - you just changed my life for the better! :-)
March 31, 2019 18:45
@Michael Kelley Thanks, I'll take a look
April 09, 2019 8:15
Been in industry for more than 7 years and never heard of it, just wow and thanks.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.