I am a novice using PowerShell 5.1 under Win 10.
I am tinkering with history. So far, I managed to have a persistent history across sessions with PSReadline
. *
But this is a "limited" persistent history, as only commands are saved in a text file, which is read when launching a new session. So a lot of information on the HistoryInfo objects is lost, in particular the StartExecutionTime. Then,
Get-Content (Get-PSReadlineOption).HistorySavePath
gives all commands with no "timestamp", andGet-History
gives commands with timestamp but only for the current session.
I am used to history
in bash
, which gets both points right.
This old doc (2009) shows a possible workaround.
I managed to export all history info before leaving a session with Get-History | Export-Clixml $env:PSDIR'\my_history.xml'
.
Executing Import-Clixml $env:PSDIR'\my_history.xml' | Add-History
at the beginning of a new session recovers the full history, including timestamps.
What is missing, I guess, is:
A way to automatically export history upon closing the session. I don't even know if this is possible.
Removing the first line in the history after importing, as it contains the importing command itself. I didn't work on this, but I guess I can handle it.Not needed. If the importing command is executed in startup scripts it does not go to history.
Is this a viable way to achieve the intended result? If so, how could I incarnate item 1?
Are there any alternatives?
* Although I could not make it work in the PS ISE.