94

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?

5 Answers 5

95

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*' }

114
  • 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 
5
  • 6
    Super useful! Note that multiple Ctrl+R presses will cycle through the results. Commented Dec 5, 2019 at 16:42
  • 3
    This is insanely useful. You're a genius, thank you. Commented Aug 2, 2020 at 19:35
  • 2
    Truly life-changing tip. Commented Mar 11, 2023 at 0:04
  • Exactly what I came here looking for. Commented Jun 25, 2024 at 17:17
  • This is incredibly poorly done. Try seeing how JPSoft Take Command does command line history. Commented Jul 15, 2024 at 22:05
5

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 }

3

I found the following more direct:

Get-History 

Alias list:

Get-Alias 

History and h are alias for Get-History
Using the following command gets me the related commands where the word "history" was used.

h | Select-String -Pattern "history" 

Results:

PS D:\temp\test> h | select-string -Pattern "history" Get-History History history -count 5 | Add-History Get-History History history -count 5 | Add-History History get-help history 
0
function Search-History { Param( [Parameter(Position = 0, Mandatory = $true)] [string]$Pattern ) $historyFilePath = (Get-PSReadlineOption).HistorySavePath Get-Content -Path $historyFilePath | Select-String -Pattern $Pattern } 

I use this function and added it to $PROFILE. Use it for example Search-History .*

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.