Automatically export history at end of powershell session
Another way to do it, just in case it's helpful for anyone.
Powershell now stores command history automatically in the file "%userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt"
To display the history, I've added a function into my profile like so...
function h2 {
$h = [System.Environment]::ExpandEnvironmentVariables("%userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt")
cat $h;
}
Now I can just type "h2" to display the full history. Not particularly clever, but effective.
Not perfect, but you can use a shortcut with a start command telling PowerShell to save all history on exit.
- Right-click your desktop and select New -> Shortcut
- Enter the following as the shortcut path:
powershell -NoExit -Command $histlog = Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action {Get-History | Export-Clixml ~\PowerShell\history.xml}
- Choose a name for your shortcut and click OK.
If you launch from this shortcut, your history will be saved to ~\PowerShell\history.xml whenever PowerShell gets an exit event.