Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active June 1, 2019 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/8f692da36d819b2c9057769b08faec89 to your computer and use it in GitHub Desktop.
Save guitarrapc/8f692da36d819b2c9057769b08faec89 to your computer and use it in GitHub Desktop.
Show Command histroy on Windows 10's Windows PowerShell via PSReadline
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