I type a lot in the Terminal; sometimes it autocompletes previous commands (PSReadline), but I often want to browse, select, and run multiple commands. In this blog post, I will show you a Function that does just that 🙂
What is PSReadline?
“The PSReadLine module contains cmdlets that let you customize the command-line editing environment in PowerShell.
There have been many updates to PSReadLine since the version that ships in Windows PowerShell 5.1.
- v2.3.6 first shipped in PowerShell 7.4.7 and 7.5.0
- v2.3.5 first shipped in PowerShell 7.4.2 and 7.5.0-preview.3
- v2.3.4 first shipped in PowerShell 7.4.0-rc.1
- v2.2.6 first shipped in PowerShell 7.3.0
- v2.1.0 first shipped in PowerShell 7.2.5
- v2.0.4 first shipped in PowerShell 7.0.11
- v2.0.0 ships in Windows PowerShell 5.1
For more information about version differences, see about_PSReadLine_Release_Notes.”
Ctrl-R and F7History also work fine?
While using Ctrl-R in a PowerShell Terminal works (backward Incremental Search) (You type Ctrl-R, type in a few characters, and press Ctrl-R again until you find what you are looking for, then hit Enter to execute that item), it’s not very handy for browsing multiple occurrences, and you can’t select multiple items to execute.
F7History works a bit better, but you can’t select multiple items to execute when you hit Enter.
Search-History
And for those reasons, I made a Search-History Function that you can use to search your Console history, or filter it to search for specific items, and by using the Spacebar (In PowerShell v7, in PowerShell v7, you can use Ctrl and the left mouse button) to select one or multiple items that will be executed after hitting Enter.
It uses Out-ConsoleGridview (PowerShell v7; the module Microsoft.PowerShell.ConsoleGuiTools will be installed if not already present) or Out-GridView (PowerShell v5; the ISE component is needed on your system for this to work). However, PowerShell v6 is not supported…
Adding it to your profile
You can download and save the script below (or from GitHub) to your system, and add it to your PowerShell profile by following the steps below:
- Start PowerShell v5 or v7 in a new Terminal
- Enter notepad $Profile (Or any other editor depending on your platform)
- Add . c:\scripts\Search-History.ps1 on a new line (Change the path to where you saved the script)
- Save and quit
Start a new Terminal in which the Search-History Function will be available.
Examples
Without filtering
If you run Search-History, it shows a GridView of your complete history, with the newest items on top. By either using the Spacebar or the Ctrl/Left-mouse button, you can select multiple items and execute them. This will look like this:

And after hitting Enter:

I added an extra line after each command to separate them, and it will show which items it executed. If one couldn’t be executed, it will report that, too.
With filtering
You can use the -Filter Parameter; you don’t need to type it because Search-History Foo will also filter the History items for the Foo keyword. For example, running Search-History Graph will look like this:

Selecting and executing items works the same:

Wrapping up
And that’s my Search-History Function 🙂 Easy to use for searching and running multiple lines from your history in your Console. Have a lovely weekend!
The script
You can copy/paste the contents below into c:\scripts\Search-History, for example.
function Search-History {
param (
[string]$Filter
)
# Exit if running in PowerShell v6, that has no Out-ConsoleGridView or Out-GridView
if ($PSVersionTable.PSVersion.Major -eq 6) {
Write-Warning "PowerShell 6 is not supported, exiting..."
return
}
# Search History file, and sort reverse so that newest item is on top
$History = [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems() | Select-Object CommandLine -Unique
[array]::Reverse($History)
# Pre-apply the filter so the empty check is accurate
if ($Filter) {
$History = $History | Where-Object { $_ -like "*$Filter*" }
}
# Out-ConsoleGridView if PowerShell v7
if ($PSVersionTable.PSVersion.Major -ge 7) {
# Check if Microsoft.PowerShell.ConsoleGuiTools is installed, install if not
if (-not (Get-Command Out-ConsoleGridView -ErrorAction SilentlyContinue)) {
Write-Warning ("Microsoft.PowerShell.ConsoleGuiTools Module was not found, installing now...")
try {
if ($PSVersionTable.PSVersion -lt [version]'7.4') {
Install-Module -Name Microsoft.PowerShell.ConsoleGuiTools -Force -Confirm:$false -ErrorAction Stop
}
else {
Install-PSResource -Name Microsoft.PowerShell.ConsoleGuiTools -Quiet -Confirm:$false -TrustRepository -ErrorAction Stop
}
Write-Host ("Microsoft.PowerShell.ConsoleGuiTools Module successfully installed") -ForegroundColor Green
}
catch {
Write-Warning ("Error installing Module, exiting...")
Write-Warning $_.Exception.Message
return
}
}
if ($History) {
$History = $History | Out-ConsoleGridView -OutputMode:Multiple -Title 'Select one, or more, lines with Spacebar. Hit Enter to execute them'
}
else {
Write-Warning ("No items found matching query, exiting...")
return
}
}
# Out-GridView is PowerShell v5
if ($PSVersionTable.PSVersion.Major -eq 5) {
if ($History) {
$History = $History | Out-GridView -OutputMode:Multiple -Title 'Select one, or more, with Ctrl+Left mousebutton. Hit Enter to execute them'
}
else {
Write-Warning ("No items found matching query, exiting...")
return
}
}
# Display and execute selected item(s) with an empty line in between
if ($History) {
Write-Host ("Executing History item(s):") -ForegroundColor Green
foreach ($SelectedItem in $History) {
Write-Host ("- {0}" -f $SelectedItem.CommandLine)
}
foreach ($Item in $History) {
Write-Host ("`n")
try {
Invoke-Expression $Item.CommandLine -ErrorAction Stop
}
catch {
Write-Warning ("Error executing selected History item: {0}" -f $Item.CommandLine)
Write-Warning $_.Exception.Message
}
}
}
else {
Write-Warning ("No History item selected...")
}
}
Download the script(s) from GitHub here.

I think that’s running it, viewing old commands and hitting Escape to exit without selecting anything?