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

  1. Use the Built-in History Search Shortcut: Press Ctrl+R to open the history search box. Type part of a command (e.g., "Get-Service") and press Enter. 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, pressing Ctrl+R and typing C:\temp will show it in the results.

    Note: This method works only for the immediate session. To search across all sessions, see Step 6.

  2. Filter Using the History | Where-Object Command: 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" }

  3. 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 like Get-ChildItem C:\Dir or Get-ChildItem *Dir*.

    For case-insensitive searches, prefix with -i: Get-History | Where-Object { $_ -iMatch "get-service" }

  4. Limit Recent Commands: To search only the most recent commands (e.g., last 20), use: Get-History -Count 20 | Where-Object { $_ -match "your-keyword" } Replace your-keyword with what you recall.

    Example: To find the last 10 commands containing Stop-Service: Get-History -Count 10 | Where-Object { $_ -match "Stop-Service" }

  5. 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.txt Open 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).

  6. 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" } Replace your-keyword with 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.

  7. 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 run Find-History -keyword "Get-Service" to list matching commands.

    Save this in your PowerShell profile (run notepad $PROFILE in PowerShell to edit it).

    If that did not work: If the above steps don’t return results, check:

    1. You’ve run at least one command (history starts empty).
    2. You’re using PowerShell 5.1 or later (older versions have limited history).
    3. Admin rights are required for cross-session searches (Step 6).
    Clear the history first with Get-History | Out-Null to reset it.

Common Mistakes

  • Using the wrong search term: History matches only exact or partial text, so be precise. For example, Get-ChildItem won’t match Get-Service. Include details like folders or objects.
  • Ignoring case sensitivity: By default, searches are case-sensitive. Use -iMatch for case-insensitive results.
  • Assuming history is global: The Ctrl+R shortcut 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+R to jump to any command in your session. If you want to modify it, edit the line in the results window before pressing Enter.
  • 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.csv Then import the file with Import-Csv C:\temp\services.csv | foreach { & $_.PSObject.Properties.Value }
  • Clear history when needed: Wipe history with: Get-History | Out-Null This 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.ps1 Then restart PowerShell and check history remains intact.

Troubleshooting

  • No history is shown:
    1. Run a command (e.g., Get-Date) to populate history.
    2. Check PowerShell version: powershell -version. Older versions (pre-5.1) have limited history support.
    3. Restart PowerShell or your PC if history persists as blank.
  • Search returns nothing:
    1. Expand the search term to include more context. For example, Get-Service * finds commands like Get-Service -Name "EventLog".
    2. Use wildcards (e.g., *Dir*) to account for variations.
    3. Check for typos in the command or history file.
  • Cross-session history fails:
    1. Run the command as Administrator (right-click PowerShell > "Run as administrator").
    2. Verify PowerShell 5.1+ is installed (try Get-History -AllSessions once).
    3. If the command still errors, check for permission denials or history export issues.
  • History exports corrupt data:
    1. Use UTF-8 encoding when exporting (as shown in Step 5).
    2. Avoid spaces or special characters in paths (e.g., C:\My Documents\file.txt).
    3. 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).
Try running the command in an admin PowerShell session or check your PowerShell version.

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.