Examples/Profile.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# import PSReadline and PSReadlineHistory
Import-Module PSReadline, PSReadlineHistory

# configure PSReadline history file
Set-PSReadlineOption -HistorySaveStyle SaveIncrementally -HistoryNoDuplicates -HistorySavePath "$([environment]::GetFolderPath('Personal'))\WindowsPowerShell\Command-History.txt"

# create popup command history search using Get-PSReadlineHistory
Set-PSReadlineKeyHandler -Key F7 -BriefDescription "PSReadlineHistoryPopup" -LongDescription "Popup PSReadline history command selector" -ScriptBlock {
    
    # get the current content of the input buffer
    $HistorySearchPattern = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $HistorySearchPattern, [ref] $null)
    $MatchSplat = @{}
    if ( $HistorySearchPattern ) {
        $MatchSplat.Match = $HistorySearchPattern
    }
    
    # get the PSReadline command history
    $HistoryCommand = Get-PSReadlineHistory @MatchSplat | Sort-Object Id -Descending | Out-GridView -Title History -PassThru | Select-Object -ExpandProperty CommandLine
    if ( $HistoryCommand ) {
        [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
        [Microsoft.PowerShell.PSConsoleReadLine]::Insert($HistoryCommand)
    }

}

# add aliases for PSReadlineHistory functions
New-Alias -Name hh   -Value Get-PSReadlineHistory
New-Alias -Name ihhy -Value Invoke-PSReadlineHistory