18

Powershell now handily remembers history from previous sessions, and I can get to earlier commands simply by using the up-arrow. What I would like though is to be able to display this history, but I can't figure out how to do it.

The command get-history for some reason only seems to be able to display the history for the current session. Even passing the -count option doesn't help.

At the moment the only way I can get to a previous command is to manually up-click through all previous commands to find the one I'm looking for. This obviously can be quite tedious if the command was run a while ago.

Is there some trick to make get-history work correctly, across sessions. This list is obviously stored somewhere, so it aught to be possible to display it.

edit: This isn't a duplicate question. This is about accessing the full history which is already recorded by Powershell, not about adding a new (custom) way to save session history. Those other questions and associated answers were relevant before Powershell had the ability to automatically record full command history.

5
  • 1
    This isn't exactly a duplicate. Powershell by default now remembers the history, which is previously couldn't do. I'm just wanting to access this already existing history. The answers for that other question are kind of hacks. There should be some other way now to do it natively. Apr 5, 2017 at 23:12
  • If you use the console host and the PSReadline module, the history is written to a text file and preserved between sessions. (PSReadline is baked in on Windows 10.) Apr 6, 2017 at 21:28
  • 1
    New versions of Powershell automatically save history to %userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt Jun 8, 2018 at 10:18
  • Nope, different question. Feb 17, 2019 at 1:08
  • 1
    Get-Content (Get-PSReadlineOption).HistorySavePath | more Feb 17, 2019 at 1:08

3 Answers 3

14

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 
}
2
  • 2
    This is a nice solution. I like the keyword search feature. Dec 11, 2019 at 22:29
  • 1
    Great solution. Much appreciated. Sorry it took so long to accept it. Jun 3, 2021 at 7:05
12

The solution commented by "charles ross" is what I was looking for:

Get-Content (Get-PSReadlineOption).HistorySavePath | more
4
  • This is a nice solution, but what if one wants to export the replies as well, such as the errors made by the wrong input of a cmdlet? It would be similar to highlighting all (CTRL+A) the input and output and then copy/paste it someplace. Thanks.
    – salvu
    Feb 13, 2021 at 18:25
  • Awesome solution! Now i'm trying to create an alias, but this gives error Set-Alias -Name hi -Value Get-Content (Get-PSReadlineOption).HistorySavePath | more -> Set-Alias : A positional parameter cannot be found that accepts argument
    – johny why
    Sep 9, 2021 at 23:27
  • And the last N items: cat (Get-PSReadlineOption).HistorySavePath | select -Last 200.
    – not2qubit
    Dec 30, 2021 at 13:31
  • Damn, why should this be this complex? It is a lot easier to do the same thing on Linux. Oct 25, 2022 at 6:19
0

These commands are also stored in a text file. You can find the location of this file by executing:

(Get-PSReadlineOption).HistorySavePath

The keyword search provided above is helpful as well.

You must log in to answer this question.

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