5

In Powershell 5.1, one could press F7 and a history of previous entered commands would show up. I just intalled Powershell 7.2 and F7 no longer does anything. F8 still autocompletes based on command history though.

DarkDiamond
  • 1,919
  • 11
  • 15
  • 21
geronimo
  • 153

2 Answers2

7

F7 no longer does anything in PowerShell 7.

By default it doesn't do anything. However read on ...

Get-PSReadLineKeyHandler lists all of the available keyboard short cuts and as of PowerShell 7 F7 is not longer included in the list of history functions:

History functions
=================
Key       Function              Description
---       --------              -----------
Alt+F7    ClearHistory          Remove all items from the command line history (not PowerShell history)
Ctrl+s    ForwardSearchHistory  Search history forward interactively
F8        HistorySearchBackward Search for the previous item in the history that starts with the current input - like PreviousHistory if the input is empty
Shift+F8  HistorySearchForward  Search for the next item in the history that starts with the current input - like NextHistory if the input is empty
DownArrow NextHistory           Replace the input with the next item in the history
UpArrow   PreviousHistory       Replace the input with the previous item in the history
Ctrl+r    ReverseSearchHistory  Search history backwards interactively

The about output is from running Get-PSReadLineKeyHandleron PowerShell 7.2.2

Oddly enough F7 is still listed in the Microsoft documentation at about History - PowerShell | Microsoft Docs so it should work.


The Plot Thickens

It does still work, it just does not work in combination with the PSReadLine module (1). I am not sure if this is documented anywhere but you can run the following:

Remove-Module -Name PSReadLine

After this cmdlet you will be able to F7 your heart out again, but you'll miss out on all the fancy colors ;)

Please note that Remove-Module does not actually delete the module, it simply unload it from the current session.

(1) emphasis mine

Source F7 History no longer in Windows 10 AU? : PowerShell

There is an open bug report at Please allow F7 to work · Issue #620 · PowerShell/PSReadLine


Workaround

You can use the following script.

You can add your own F7 in PSReadline by using the Set-PSReadlineKeyHandler cmdlet in a script. Example:

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
      }
    }
  )
  $command = $history | Out-GridView -Title History -PassThru
  if ( $command ) {
    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert(($command -join "`n"))
  }
}

When you use this function, the F7 key will appear in a pop-up grid view window. Select a history entry and press Enter, and PowerShell will put it on the command line for editing.

Source Make Command-History Pop-Up work via F7 in Windows 10 Powershell - Stack Overflow, answer by Bill_Stewart

See also Use F7 as "Show Command History" in Powershell for a similar workaround.

DavidPostill
  • 162,382
0

I developed the "F7 History" PowerShell Module to address this:

Install F7History from the PowerShell Gallery.

Install-Module -Name "F7History"

Add a line to import F7History in your PowerShell $profile:

Import-Module -Name "F7History"

At the PowerShell command line:

  • Press F7 to see the history for the current PowerShell instance.
  • Press Shift-F7 to see the history for all PowerShell instances.

Whatever was typed on the command line before hitting F7 or Shift-F7 will be used for the Out-ConsoleGridView `-Filter`` parameter.

When the Command Line History window is displayed:

  • Use the arrow keys or mouse to select an item.
  • Use ENTER to insert the selected item on the command line.
  • Use ESC to close the window without inserting anything on the command line.
tig
  • 109
  • 3