Skip to content

Instantly share code, notes, and snippets.

@T99
Last active August 25, 2022 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save T99/b1fbba5c27c8c316e0f01d82e324c9f1 to your computer and use it in GitHub Desktop.
Save T99/b1fbba5c27c8c316e0f01d82e324c9f1 to your computer and use it in GitHub Desktop.
Preserve/save PowerShell command history globally, across sessions.
# The full path at which to save the history file.
$history_file = "~\PowerShell\history.csv"
# The amount of history to save.
$history_size = 32KB
# Create the ~\PowerShell directory if is does not already exist.
if (!(Test-Path ~\PowerShell -PathType Container)) {
New-Item ~\PowerShell -ItemType Directory
}
# Load back the global history into the session history
if (Test-Path $history_file) {
Import-CSV $history_file | Add-History
}
# Register an event to automatically save history on exit.
Register-EngineEvent PowerShell.Exiting -SupportEvent -Action {
Get-History -Count $history_size | Export-CSV $history_file
}
@T99
Copy link
Author

T99 commented Aug 25, 2022

@Dr-WaSaBi
Copy link

Wow... awesome.. copying this right now...
Thanks
Russ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment