Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Command-History Pop-Up work via F7 in Windows 10 Powershell [closed]

In a Powershell console, on Windows 10 (10.0.17134.0), F7 does not pop-up the Command History like it does for CMD console. How could we make this work in a PS console window? Any hints on a $Profile syntax to do so?

like image 276
Howard Hoffman Avatar asked May 16 '18 17:05

Howard Hoffman


People also ask

How do I check command history after closing?

Check Command History With Doskey Here's what you need to do: Launch the Command Prompt from the Start menu and type the following the pop-up window: “ doskey /history “ Press “Enter.” The commands you've executed in the active session will appear in the Command Prompt window.

How do I enable PowerShell history?

When you're at the PowerShell console, just run the history command and it will display all commands you typed during the current session. File Explorer will open to your specified location and you can see a text file named ConsoleHost_history. txt . Open the ConsoleHost_history.

How do I get command prompt history?

Using the Command History menu: Open the Start menu and type cmd in the search bar. Click on the Command Prompt app to proceed. On the Command Prompt home screen, press the F7 key to access the menu which has all of your previously executed commands listed in chronological order.

Does PowerShell have a history command?

Description. The Get-History cmdlet gets the session history, that is, the list of commands entered during the current session. PowerShell automatically maintains a history of each session. The number of entries in the session history is determined by the value of the $MaximumHistoryCount preference variable.


2 Answers

As you discovered, this is because the PSReadline module is installed by default on Windows 10. 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.

like image 87
Bill_Stewart Avatar answered Oct 28 '22 20:10

Bill_Stewart


Yes, one work around is: https://www.reddit.com/r/PowerShell/comments/4x5iig/f7_history_no_longer_in_windows_10_au/.

Run Remove-Module -Name PSReadLine from the command-line. It will remove the PSReadLine module from that session. That let's F7 work again to display the history.

like image 41
Howard Hoffman Avatar answered Oct 28 '22 20:10

Howard Hoffman