Skip to content

Instantly share code, notes, and snippets.

@hoest
Created October 23, 2012 11:47
Show Gist options
  • Save hoest/3938346 to your computer and use it in GitHub Desktop.
Save hoest/3938346 to your computer and use it in GitHub Desktop.
Keep Powershell history
# profile folder
$profileFolder = split-path $profile
# save last 100 history items on exit; with the 'exit' command
$historyPath = Join-Path $profileFolder "ps_history.xml"
# hook powershell's exiting event and hide the registration with -supportevent.
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action {
Get-History -Count 100 | Export-Clixml (Join-Path $profileFolder "ps_history.xml") }
# load previous history, if it exists
if((Test-Path $historyPath)) {
Import-Clixml $historyPath | ? { $count++; $true } | Add-History
Write-Host -Fore Green "Loaded $count history item(s).`n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment