22

In bash I can do Ctrl+R and start typing a command to have it search my history.

How do I do that in powershell?

Is there a way to bind a key to something like?

function GH($str) {get-history | select-string $str}

But I would need to re-read the string for each char input or removed to output matching commands like Ctrl+R does.

Is this possible?

1
  • Related: For cmd.exe, there is the tool clink.
    – koppor
    Jun 28, 2020 at 12:23

5 Answers 5

13

You can fix this by adopting the PSReadLine module.

See an article on the Hey, Scripting Guy! blog regarding this bash inspired readline implementation for PowerShell.

In particular, the usage of Ctrl+Alt+(Shift)+? to show all keybindings.

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.0.10240.16384
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.42000
BuildVersion                   10.0.10240.16384
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3

Ctrl+R

PS C:\> Get-Module -ListAvailable
bck-i-search: mo_

Ctrl+Alt+(Shift)+S

Key                  Function                 Description
---                  --------                 -----------
Ctrl+r               ReverseSearchHistory     Search history backwards interactively
Ctrl+s               ForwardSearchHistory     Search history forward interactively

In short: either install this module by hand or upgrade to Windows PowerShell v5.

9

Type your search term and then press F8, this will search your command history.

There's a very useful list of PowerShell shortcuts here.

2
  • Have you tried that yourself? It never really worked for me or only half the time. For example I type a bunch of get-<whatever> commands. Then type get-<F8> and it only cycles through one or two random commands from my history? I don't know what the hell its doing. For example I just typed get-childitem and ran it then immediately after type get-c<f8> and I get nothing. Maybe my session is messed up?
    – red888
    Mar 7, 2016 at 20:42
  • Works 100% of the time for me, this is with WMF 5 installed
    – Sam Cogan
    Mar 7, 2016 at 20:45
6

I don't know when this got added, but on my non-customized, updated, Windows 10, it's built in. Hit ctrl+r for reverse history search, it works great.

enter image description here

2
  • I also work on win10. Ctrl+R works in powershell, but does not work in cmd window.
    – dudu
    Apr 14, 2019 at 2:24
  • Sure, but why would you need to use cmd.exe?? Jan 15, 2020 at 14:42
0

There's also graphical search under F7 key. When pressed it will show popup which you can scroll to search for command which was executed earlier. Pressing enter while cursor hover the line will reexecute it again.

0

You can also start your command by putting a # character in front of the partial search text of the command you are looking for, then press tap afterwards (multiple times if necessary) to search through session history.

You must log in to answer this question.

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