PSReadLine icon indicating copy to clipboard operation
PSReadLine copied to clipboard

Feature Request: `Get-History` command showing history from previous sessions

Open CarltonSemple opened this issue 7 years ago • 13 comments

Apparently there are community-provided solutions for this, but it would be nice to have a built-in solution for saving command history across sessions, similar to Bash.

@jpsnover https://twitter.com/jsnover/status/919145208255799296

CarltonSemple avatar Oct 14 '17 19:10 CarltonSemple

Unless I'm doing something special I'm aware of, doesn't PSReadLine do this by default in Core?

Can you provide what specific bash-like functionality you are looking for?

markekraus avatar Oct 14 '17 19:10 markekraus

Yes, PSReadLine does this by default.

lzybkr avatar Oct 14 '17 19:10 lzybkr

So, i downloaded and tried PowerShell 6.0.0-beta.7. The desired functionality would be that the history command would show the history from the previous session, without any additional work

CarltonSemple avatar Oct 14 '17 19:10 CarltonSemple

@CarltonSemple Correct. Assuming you don't have something in your profile that is disabling PSReadLine, and assuming you are not making any modifications to the default install 6.0.0-beta.7 and 6.0.0-beta.8 should maintain CLI history between sessions.

markekraus avatar Oct 14 '17 19:10 markekraus

@markekraus I deleted my profile files just now, and tried 6.0.0-beta.8 .... history and Get-History still don't show CLI history from the previous session for me

CarltonSemple avatar Oct 14 '17 20:10 CarltonSemple

I'm using Windows 10

CarltonSemple avatar Oct 14 '17 20:10 CarltonSemple

@CarltonSemple ah Get-History only provides commands for the current session. However you can use the up arrow to access the commands from the previous session. and there are more options such as searching and such. I'm not a PSReadLine expert.

markekraus avatar Oct 14 '17 20:10 markekraus

You can search backwards with Ctrl+r. If you go too far, use Ctrl+s to move forward through the matching history list.

rkeithhill avatar Oct 14 '17 20:10 rkeithhill

@markekraus I guess maybe I should change the title to specifically reference the Get-History command, then.

CarltonSemple avatar Oct 14 '17 20:10 CarltonSemple

Possibly but the point, I think, is that PSReadline (inspired by GNU Readline), was created to provide such features. Get-History is legacy command that is still useful IMO as more of "audit" trail for the current session. Back in the old days, I use to save the Get-History history to files by adding this to my profile:

$historyPath = Join-Path (Split-Path $profile) "history-$(Get-Date -f o)-$pid.clixml"
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action {
    Get-History | Export-Clixml $historyPath
}.GetNewClosure()

I'm not sure if this will work on PowerShell Core. I also had find functions I had defined that could search these history files. I don't use these so much anymore because PSReadline scratched that itch pretty nicely.

rkeithhill avatar Oct 14 '17 20:10 rkeithhill

I think some parity with bash's history from PSReadLine would be nice to have. I definitely have done history | more a million times trying to trace back the command history to see where things went wrong or when I'm trying to remember a command I don't recall enough of the syntax to make keyword searching the history useful.

markekraus avatar Oct 14 '17 20:10 markekraus

@SteveL-MSFT @daxian-dbw Could you please make a conclusion - should we enhance Get-History cmdlet in the repo (change label to Area-Cmdlets-Core) or PSReadline (in the case please move the issue in PSReadline repo)?

iSazonov avatar Sep 28 '19 17:09 iSazonov

See also the "Dangers" of this indefinite history. https://www.windowsmanagementexperts.com/the-danger-of-default-settings-the-psreadline-module/the-danger-of-default-settings-the-psreadline-module.htm

espoelstra avatar Dec 04 '19 17:12 espoelstra