Search code examples
powershellpsreadlinecommand-history

PowerShell track history as full objects across sessions


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,

  1. Get-Content (Get-PSReadlineOption).HistorySavePath gives all commands with no "timestamp", and
  2. Get-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:

  1. A way to automatically export history upon closing the session. I don't even know if this is possible.

  2. 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.


Solution

  • Thanks to the pointer by RetiredGeek, I could move forward. What I did:

    1. Added Register-EngineEvent PowerShell.Exiting -Action { . $env:PSDIR'\history\save_history.ps1' } to my profile.ps1.

    2. Created file $env:PSDIR'\history\save_history.ps1' containing Get-History | Export-Clixml $env:PSDIR'\my_history.xml'

    This almost provides a solution. The only two remaining issues are:

    1. Removing the exit command that is left as the last command in history. This seems manageable.

    2. Make this work when closing by hitting "X".* In this case, I have observed a strange result. If I close session #1 with "X", and open a new session #2, the commands that I have entered in session #1 do not show up at the bottom of Get-History. Nevertheless, they are available with Up/Dpwn arrows. This is likely worth another question.


    * As per this, starting with PS v3 the "X" button was hooked up with the exit event. But "X" does not seem to work exactly like exit, given point 2 above.