The quickest way to check PowerShell command history is with the built-in command History or Type History—these directly display your logged commands without extra setup. If you need deeper inspection (like command-line arguments or execution time), you can use Get-History or export it to a file instead. No additional tools or permissions are required for basic viewing, though some advanced features may need elevated permissions or proper script logging.

What You'll Need

  • An active PowerShell session (either Windows PowerShell or PowerShell Core, version 5.1 or later)
  • If exporting history, a destination folder (optional, for writing to disk)
  • Basic knowledge of PowerShell command-line navigation (arrow keys, Ctrl+R, etc.)

Step-by-Step

  1. Open PowerShell: Launch either:
    • Windows PowerShell: Search for "Windows PowerShell" in the Start menu, then run it.
    • PowerShell Core (cross-platform): Open the terminal application (e.g., Windows Terminal, VS Code, or macOS/Linux terminal) and type powershell.
    Verification: Check the prompt at the top-left corner—it should read PS <ComputerName> > (Windows) or > (PowerShell Core). If it’s an older version, upgrade to 5.1+ via winget install Microsoft.PowerShell or download from Microsoft’s site.
  2. View the full history: Type either of these commands to display your command history:
    • History (shows a numbered list of the last 1,000 commands)
    • 10 | History (shows only the top 10 commands)
    • Type History (opens a new window with formatted history)
    Actionable tip: Press Enter after any command to add it to history. Press Ctrl+R to search for past commands by partial text.
  3. Filter or inspect specific commands: Use PowerShell’s built-in filtering:
    • History -Count 50 (shows the last 50 commands)
    • History -Filter "Get-Service" (finds all commands containing "Get-Service")
    • History -Count 10 | Format-Table -AutoSize (displays formatted output)
    Advanced note: For full metadata (e.g., time, arguments), use: Get-History -Count 10 | Select-Object TimeStamp, CommandLine
  4. Export history to a file: To save your history for future reference:
    1. Run Get-History | Export-Clixml -Path "C:\Temp\PowerShellHistory.xml" (Windows).
    2. Run Get-History | Select-Object CommandLine | Export-Csv -Path "C:\Temp\PowerShellCommands.csv" (CSV format).
    Note: This requires System.IO permissions, which are usually granted in PowerShell Core but may prompt for admin rights in Windows PowerShell.
  5. Clear or reset history: To wipe your history (use with caution!):
    1. Run Get-History | Remove-Item -Force (deletes in-memory history).
    2. For persistent clearing, edit $PSReadLine.Options.AllowHistoryRebirth = $false in your profile file (located at ~/.profile or $HOME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 for Windows).

Common Mistakes

  • Assuming history is saved permanently: PowerShell history is stored only in memory (1,000 commands max) unless explicitly exported or logged via Set-ItemProperty in the profile. Clearing history won’t affect past sessions unless you saved it.
  • Using old PowerShell versions: Pre-5.1 versions lack robust history features. Upgrade via winget upgrade Microsoft.PowerShell or choco upgrade powershell.
  • Ignoring region-specific prompts: On Windows, if you see "PSReadLine" warnings, run Set-PSReadLineOption -HistorySearchBehavior 2 to enable full history search.
  • Running export commands in restricted shells: Services like Task Scheduler may not have full history access. Export only after logging into a regular PowerShell session.

Tips

  • Shortcut keys:
    • Ctrl+R → Search past commands (type a partial command text).
    • Up/Down arrow → Navigate command history interactively.
    • Ctrl+Shift+R → Repeat the last edited command.
  • Log history permanently: Add this to your profile (edit $PROFILE) to log all commands to a file: function Log-History { & "$env:PSReadLine\HistoryFile" -Append "$($args -join ' ')" } Add-HostCommand -Name Log-History -Function Log-History
  • Filter by time: Use Get-History | Where-Object { $_.TimeStamp -ge '2023-01-01' } to review recent commands.
  • Compare sessions: Run Get-History -All | Export-Clixml "AllHistory.xml" to dump ALL history (may take time for large sessions).
  • Avoid history conflicts: If multiple users share a computer, export history individually (e.g., per user profile).

Troubleshooting

  1. History not showing up: Check if PowerShell is running in an elevated session (admin rights are needed for some export operations). Try:
    • Restart PowerShell.
    • Run Get-History | Select-Object Count—if Count is 0, your session has no history.
  2. Export fails with "Access Denied":
    • Run the export command in an admin PowerShell session.
    • Create the target folder first: New-Item -ItemType Directory -Path "C:\Temp".
    • If using PowerShell Core, ensure System.IO is available (run Import-Module System.IO).
  3. History window doesn’t open: Ensure you’re using PowerShell 5.1+ or PowerShell Core. For Windows PowerShell, run: Get-WinEvent -FilterHashtable @{LogName='System'; ProviderName='Microsoft-PowerShell'} to check for errors.
  4. Commands appear garbled in history: This often happens if PowerShell is running in a restricted context (e.g., a service). Check:
    • Your session type (regular or administrative).
    • The exact command prompt (e.g., some cloud/remote sessions truncate output).
  5. History resets after crash: This happens when the session terminates. To prevent this:
    1. Use a persistent profile script (e.g., Set-PSReadLineOption -HistorySaveEnabled $true).
    2. Export history to disk during each session (step 3 above).

Frequently Asked Questions

Why won’t my PowerShell history show up?

History only appears if commands were executed in the current session. If you’re using a script or remote session, check if the session is interactive (not a batch file or script block). Some profiles or scripted environments strip history.

How do I check history in PowerShell Core vs. Windows PowerShell?

Both use the same commands (History, Get-History). However, in PowerShell Core, you may need to enable PSReadLine first: Set-PSReadLineOption -HistorySearchBehavior 2. In Windows PowerShell, ensure PSReadLine is installed via Install-Module PSReadLine.

Can I see arguments (flags) in the history?

Yes, use Get-History -Count 10 | Format-Table -AutoSize to display full command lines. For more details, pipe to Format-List or Select-Object CommandLine.

How do I permanently disable PowerShell history?

Edit your profile file (located in the user’s PowerShell directory) and add: $PSReadLine.Options.AllowHistoryRebirth = $false or restart PowerShell after clearing the history cache.

Why is my history empty after upgrading to PowerShell 7?

PowerShell Core (7+) stores history differently. To reset: Remove-Item -Force $env:PSReadLine\HistoryFile Then restart PowerShell and test with History.

How can I see history from a previous session?

There’s no built-in way, but you can dump all history for past sessions via: Get-History -All | Export-Clixml "AllSessionsHistory.xml" Note this may be large and may not contain commands from older sessions if they didn’t use this method.