I use current Windows 10 with Powershell 5.1. Often, I want to look up commands I have used in the past to modify and/or re-run them. Inevitably, the commands I'm looking for were run in a previous or different PowerShell window/session.

When I hammer the key, I can browse through many, many commands from many, many sessions, but when I try to search through them using Get-History | Where-Object {$_.CommandLine -Like "*docker cp*"}, I get no results. Basic troubleshooting reveals that Get-History doesn't show anything from previous sessions, as shown by:

C:\Users\Me> Get-History

  Id CommandLine
  -- -----------
   1 Get-History | Where-Object {$_.CommandLine -Like "*docker cp*"}

How can I search through the previous commands that the key provides using Get-History or another Cmdlet?


The persistent history you mention is provided by PSReadLine. It is separate from the session-bound Get-History.

The history is stored in a file defined by the property (Get-PSReadlineOption).HistorySavePath. View this file with Get-Content (Get-PSReadlineOption).HistorySavePath, or a text editor, etc. Inspect related options with Get-PSReadlineOption. PSReadLine also performs history searches via ctrl+r.

Using your provided example:

Get-Content (Get-PSReadlineOption).HistorySavePath | ? { $_ -like '*docker cp*' }

  • Press Ctrl+R and then start typing, to search backward in history interactively. This matches the text from anywhere in the command line. Press Ctrl+R again to find next match.
  • Ctrl+S works like above, but searches forward in history. You can use Ctrl+R/Ctrl+S to go back and forth in search results.
  • Type a text and then press F8. This searches for the previous item in the history that starts with the current input.
  • Shift+F8 works like F8, but searches forward.

More Info

As @jscott mentioned in his/her answer, PowerShell 5.1 or higher in Windows 10, uses the PSReadLine module to support command editing environment. The full key mapping of this module can be retrieved by using Get-PSReadLineKeyHandler cmdlet. To view all the key mappings related to history, use the following command:

Get-PSReadlineKeyHandler | ? {$_.function -like '*hist*'}

and here is the output:

History functions
Key       Function              Description
---       --------              -----------
Alt+F7    ClearHistory          Remove all items from the command line history (not PowerShell history)
Ctrl+s    ForwardSearchHistory  Search history forward interactively
F8        HistorySearchBackward Search for the previous item in the history that starts with the current input - like
                                PreviousHistory if the input is empty
Shift+F8  HistorySearchForward  Search for the next item in the history that starts with the current input - like
                                NextHistory if the input is empty
DownArrow NextHistory           Replace the input with the next item in the history
UpArrow   PreviousHistory       Replace the input with the previous item in the history
Ctrl+r    ReverseSearchHistory  Search history backwards interactively
  • 2
    Super useful! Note that multiple Ctrl+R presses will cycle through the results. – Ohad Schneider Dec 5 '19 at 16:42
  • 1
    This is insanely useful. You're a genius, thank you. – Sabuncu Aug 2 '20 at 19:35

I have this in my PS profile:

function hist { $find = $args; Write-Host "Finding in full history using {`$_ -like `"*$find*`"}"; Get-Content (Get-PSReadlineOption).HistorySavePath | ? {$_ -like "*$find*"} | Get-Unique | more }

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.