Last active
August 25, 2022 16:18
-
-
Save T99/b1fbba5c27c8c316e0f01d82e324c9f1 to your computer and use it in GitHub Desktop.
Preserve/save PowerShell command history globally, across sessions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
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
Credit to: