PowerShell stores your command history so you can revisit previous commands with a single shortcut or command. Instead of retyping or scrolling through a long list, you can search and reuse past commands instantly—saving time and reducing errors. This guide shows you exactly how to search, filter, and leverage your PowerShell history efficiently, with step-by-step methods for different scenarios.
What You’ll Need
- A Windows PC with PowerShell installed (built into Windows 10/11).
- Knowledge of basic PowerShell syntax (optional but helpful).
- A few commands already run in PowerShell (the history feature works best when you have a recent command history).
Step-by-Step How to Search PowerShell Command History
- Use the Built-in History Search Shortcut:
Press
Ctrl+Rto open the history search box. Type part of a command (e.g., "Get-Service") and pressEnter. PowerShell will cycle through matching entries, showing the full line before each. If you want to run the command with modifications, edit it manually and execute it again.Example: If you ran
Get-ChildItem C:\temp | Where-Object Length -gt 1048576, pressingCtrl+Rand typingC:\tempwill show it in the results.Note: This method works only for the immediate session. To search across all sessions, see Step 6.
- Filter Using the
History | Where-ObjectCommand: Run this one-liner to list all commands containing a keyword (e.g., "user"):Get-History | Where-Object { $_ -match "user" }Replace "user" with your search term. The output shows only matching commands with their full lines, making it easy to pick one and run it.Example: If you need to find any command involving
Get-Content, use:Get-History | Where-Object { $_ -match "Get-Content" } - Use Wildcards for Broad Searches:
If you’re unsure of the exact command but remember part of it, use wildcards (*) to match patterns:
Get-History | Where-Object { $_ -match "Get-ChildItem *Dir*" }This finds commands likeGet-ChildItem C:\DirorGet-ChildItem *Dir*.For case-insensitive searches, prefix with
-i:Get-History | Where-Object { $_ -iMatch "get-service" } - Limit Recent Commands:
To search only the most recent commands (e.g., last 20), use:
Get-History -Count 20 | Where-Object { $_ -match "your-keyword" }Replaceyour-keywordwith what you recall.Example: To find the last 10 commands containing
Stop-Service:Get-History -Count 10 | Where-Object { $_ -match "Stop-Service" } - Export and Reuse History:
To save frequently used commands for later, export the history to a file:
Get-History | Out-File -Encoding UTF8 C:\temp\powershell_history.txtOpen the file later to paste commands directly. To run a specific command from the file, edit it into PowerShell and execute.Note: This method captures the session’s history but doesn’t replicate the environment (like variables or profiles).
- Search Across All PowerShell Sessions:
To explore history from all active sessions (Windows 10/11 only), use:
Get-History -AllSessions | Where-Object { $_ -match "your-keyword" }Replaceyour-keywordwith your target term. This requires PowerShell 5.1+ and must be run as Administrator.Warning: Running this as a non-admin user may return limited or incomplete results. If it fails, check admin permissions or use Step 5 for a session-specific approach.
- Use Aliases or Custom Functions:
For automation, create a function to simplify history searches. Example:
function Find-History { param([string]$keyword) Get-History | Where-Object { $_ -match $keyword } | Select-Object -ExpandProperty Content }Then runFind-History -keyword "Get-Service"to list matching commands.Save this in your PowerShell profile (run
notepad $PROFILEin PowerShell to edit it).If that did not work: If the above steps don’t return results, check:
- You’ve run at least one command (history starts empty).
- You’re using PowerShell 5.1 or later (older versions have limited history).
- Admin rights are required for cross-session searches (Step 6).
Get-History | Out-Nullto reset it.
Common Mistakes
- Using the wrong search term: History matches only exact or partial text, so be precise. For example,
Get-ChildItemwon’t matchGet-Service. Include details like folders or objects. - Ignoring case sensitivity: By default, searches are case-sensitive. Use
-iMatchfor case-insensitive results. - Assuming history is global: The
Ctrl+Rshortcut only works in the current session. Cross-session searches require admin rights (Step 6). - Overlooking session environment: Commands exported to a file lose variables or profile settings. Manually edit them before running.
Tips
- Shortcut for quick recall: Press
Ctrl+Rto jump to any command in your session. If you want to modify it, edit the line in the results window before pressingEnter. - Save time with aliases: Create aliases for common PowerShell commands (e.g.,
gi="Get-ChildItem"**) to avoid typing long names repeatedly. - Use history in scripts: Append commands to a script file (e.g.,
C:\temp\commands.ps1) and run them later. Example:Get-Service | Export-Csv -Path C:\temp\services.csvThen import the file withImport-Csv C:\temp\services.csv | foreach { & $_.PSObject.Properties.Value } - Clear history when needed: Wipe history with:
Get-History | Out-NullThis resets the session’s command list. - Enable history persistence: To save history between sessions, add this to your PowerShell profile:
$PROFILE | Out-File -Encoding UTF8 $HOME\.psconfig\profile.ps1Then restart PowerShell and check history remains intact.
Troubleshooting
- No history is shown:
- Run a command (e.g.,
Get-Date) to populate history. - Check PowerShell version:
powershell -version. Older versions (pre-5.1) have limited history support. - Restart PowerShell or your PC if history persists as blank.
- Run a command (e.g.,
- Search returns nothing:
- Expand the search term to include more context. For example,
Get-Service *finds commands likeGet-Service -Name "EventLog". - Use wildcards (e.g.,
*Dir*) to account for variations. - Check for typos in the command or history file.
- Expand the search term to include more context. For example,
- Cross-session history fails:
- Run the command as Administrator (right-click PowerShell > "Run as administrator").
- Verify PowerShell 5.1+ is installed (try
Get-History -AllSessionsonce). - If the command still errors, check for permission denials or history export issues.
- History exports corrupt data:
- Use UTF-8 encoding when exporting (as shown in Step 5).
- Avoid spaces or special characters in paths (e.g.,
C:\My Documents\file.txt). - Test the export by reading the file later with a text editor.
Frequently Asked Questions
How do I search PowerShell history for a specific folder path?
Use this command to find commands containing the path:
Get-History | Where-Object { $_ -match "C:\path\to\folder*" }
For example, to find commands in C:\Users\Doc:
Get-History | Where-Object { $_ -match "C:\Users\Doc" }
Can I search PowerShell history in Linux/macOS?
No. Linux/macOS use different shells (Bash, Zsh) with their own history systems. Use history in Bash or fc to view/reuse commands.
Why does my cross-session search return empty results?
This happens if:
- You’re not running the command as Administrator.
- PowerShell 5.1 or later isn’t installed.
- History hasn’t been exported across sessions (only current sessions load by default).
How do I make PowerShell history persist between sessions?
Add this to your PowerShell profile (open with notepad $PROFILE):
$pshistory = Get-PSHistory -Count 1000
$pshistory | Out-File -Encoding UTF8 -FilePath $HOME\.psconfig\persistent_history.txt
Restart PowerShell to see history loaded from the file.
Is there a way to search history in an older PowerShell version?
Yes. Use history in an older PowerShell session (pre-5.1) to view commands:
history
For searches, use history | select -last 10 | findstr "your-keyword" (findstr is a Windows command). These methods are less efficient but work in legacy PowerShell.
How do I undo a command from history once I’ve run it?
If you need to re-execute a modified version of a previous command, edit the result from Ctrl+R or history export before running it. There’s no built-in "undo" feature for history commands.