Last active
June 1, 2019 11:59
-
-
Save guitarrapc/8f692da36d819b2c9057769b08faec89 to your computer and use it in GitHub Desktop.
Show Command histroy on Windows 10's Windows PowerShell via PSReadline
This file contains 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
Set-PSReadlineKeyHandler -Key F7 -BriefDescription "History" -LongDescription "Show command history" -ScriptBlock { | |
$pattern = $null | |
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $pattern, [ref] $null) | |
if ( $pattern ) { | |
$pattern = [Regex]::Escape($pattern) | |
} | |
$history = [System.Collections.ArrayList] @( | |
$last = "" | |
$lines = "" | |
foreach ( $line in [System.IO.File]::ReadLines((Get-PSReadlineOption).HistorySavePath) ) { | |
if ( $line.EndsWith('`') ) { | |
$line = $line.Substring(0, $line.Length - 1) | |
$lines = if ( $lines ) { "$lines`n$line" } else { $line } | |
continue | |
} | |
if ( $lines ) { | |
$line = "$lines`n$line" | |
$lines = "" | |
} | |
if ( ($line -cne $last) -and ((-not $pattern) -or ($line -match $pattern)) ) { | |
$last = $line | |
$line | |
} | |
} | |
) | |
# acs | |
# $command = $history | Out-GridView -Title History -PassThru | |
# desc | |
$command = $history | Sort-Object -Descending | Out-GridView -Title History -PassThru | |
if ($command) { | |
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() | |
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(($command -join "`n")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment