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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With