#PSTip Automatically persist history

Note: This tip requires PowerShell 2.0 or later.

By leveraging engine events and PowerShell profile scripts we can automatically persist command history to disk, and import it in the next session.

This is the code you can add to your PowerShell profile script:

$PSHistoryPath = Join-Path (Split-Path -Path $profile -Parent) 'PS-History.csv'
if (Test-Path -Path $PSHistoryPath) {
Import-Csv -Path $PSHistoryPath | Add-History
Register-EngineEvent -SupportEvent PowerShell.Exiting -Action {
Get-History -Count 25 | Export-Csv -Path $PSHistoryPath

The code in the action block of Register-EngineEvent will be run when PowerShell exits. New in PowerShell 4.0 and later is that the event also is triggered when you exit the PowerShell host by using “Close Window” or the “X”-button. In earlier versions the event was triggered only when using the exit command. The –SupportEvent parameter hides the subscription. Without this parameter, the subscription would be visible when running both Get-Job and Get-EventSubscriber, and we do not want to clutter the session with a profile supporting event.

In the above code we are persisting the last 25 items in the history. You can obviously edit or remove this number, but I would recommend to set a limit in order to not get a history that is too long. Remember that the history from the previous session is imported, thus over time the history will become very large if we do not configure a limit during history export.

About the author: Jan Egil Ring

Jan Egil works as a Lead Architect at Crayon, Norway. He mainly works with automation, and has a strong passion for PowerShell. He has been working with Microsoft infrastructure products such as Windows Server & System Center since the early 2000s. In the recent years the focus has been more and more related to cloud technologies in Microsoft Azure and hybrid environments. He is a multiple-year recipient of Microsoft Most Valuable Professional Award for his contributions in the Windows PowerShell and Cloud & Datacenter Management technical communities. He speaks regularly at user groups and conferences, such as Nordic Infrastructure Conference (NIC), PowerShell Conference Europe and PowerShell Summit. He is a co-organizer of Azure User Group Norway as well as the MTUG (Microsoft Technology User Group) Script Club which focuses mainly on PowerShell. You can follow him on Twitter @JanEgilRing.

Related Posts