12
votes

I found this excellent looking method of persisting history in Windows PowerShell.

# Persistent History
# Save last 200 history items on exit
$MaximumHistoryCount = 200
$historyPath = Join-Path (split-path $profile) history.clixml

# Hook powershell's exiting event & hide the registration with -supportevent (from nivot.org)
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action {
    Get-History -Count $MaximumHistoryCount | Export-Clixml (Join-Path (split-path $profile) history.clixml)
}

# Load previous history, if it exists
if ((Test-Path $historyPath)) {
    Import-Clixml $historyPath | ? {$count++;$true} | Add-History
    Write-Host -Fore Green "`nLoaded $count history item(s).`n"
}

# Aliases and functions to make it useful
New-Alias -Name i -Value Invoke-History -Description "Invoke history alias"
Rename-Item Alias:\h original_h -Force
function h { Get-History -c  $MaximumHistoryCount }
function hg($arg) { Get-History -c $MaximumHistoryCount | out-string -stream | select-string $arg }

However, when I paste this into my $PROFILE and restart PowerShell I get the following error:

Register-EngineEvent : Missing an argument for parameter 'Action'. Specify a parameter of type
'System.Management.Automation.ScriptBlock' and try again.
At D:\path\to\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:20 char:73
+ Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action
+                                                                         ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Register-EngineEvent], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.RegisterEngineEventCommand
3
FYI, PowerShell v3 fixes a number of issues you're trying to work around. $MaximumHistoryCount is 4096 by default and by default Get-History returns all items. - Keith Hill
> $PSVersionTable.PSVersion Major = 3, Minor = 0, Build = -1, Revision = -1 - alexleonard
Cool. Then you can skip modifying $MaximumHistoryCount and Get-History will return all history for you (well, up to 4096 items). Unless that is, you want to limit how much is saved. :-) - Keith Hill
Here's my history export/import feel free to steal :P - Andy Arismendi
@AndyArismendi you don't have a license stated anywhere, so I can't use it - Demi

3 Answers

10
votes

Very related to this question is "How do I access my history using the up/down arrows?" The PSReadLine module solves this:

Install-Package PSReadLine

Then add:

Import-Module PSReadLine

To your $profile.

3
votes

Well, as a combination of the topicstarter's code and a slightly changed answer by Steven Penny, this is the full piece of code which is working for me

################# Persistent History ############
# Save last 200 history items on exit
$MaximumHistoryCount = 200    
$historyPath = Join-Path (split-path $profile) history.clixml

# Hook powershell's exiting event & hide the registration with -supportevent (from nivot.org)
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action {
      Get-History | Export-Clixml $historyPath
}.GetNewClosure()

# Load previous history, if it exists
if ((Test-Path $historyPath)) {
    Import-Clixml $historyPath | ? {$count++;$true} | Add-History
    Write-Host -Fore Green "`nLoaded $count history item(s).`n"
}

# Aliases and functions to make it useful
New-Alias -Name i -Value Invoke-History -Description "Invoke history alias"
Rename-Item Alias:\h original_h -Force
function h { Get-History -c  $MaximumHistoryCount }
function hg($arg) { Get-History -c $MaximumHistoryCount | out-string -stream | select-string $arg }
1
votes

This is the code I used to use when I persisted my history:

$historyPath = Join-Path (split-path $profile) "history-$(Get-Date -f o).clixml"
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action {
    Get-History | Export-Clixml $historyPath
}.GetNewClosure()

The GetNewClosure() is used to capture the $historyPath variable IIRC.