Unlike the traditional Command Prompt, PowerShell has a dedicated location where it stores command history. This functionality is similar to how Linux bash history (.bash_history) works, allowing you to recall and manage previous commands. Here's how to work with PowerShell's command history:

Requirement: Starting in PowerShell v5 with the PSReadLine module ( Use command to install: Install-Module -Name PSReadLine -AllowClobber -Force ) loaded on Windows 10 or if you download the new PowerShell from Github: https://github.com/PowerShell/PowerShell

Change How PowerShell Command History is Saved

By default, PowerShell saves command history incrementally. However, you can change this behavior using the Set-PSReadlineOption cmdlet, note: only works within the current user context and appears to apply only to the current session without persisting:

  • Save Incrementally (Default): Saves each command as you enter it.
Set-PSReadlineOption -HistorySaveStyle SaveIncrementally
  • Save at Exit: Saves all commands only when you exit the session.
Set-PSReadlineOption -HistorySaveStyle SaveAtExit
  • Save Nothing: Disables saving command history completely.
Set-PSReadlineOption -HistorySaveStyle SaveNothing

Get the PowerShell Command History File Location

The PowerShell history file is normally under:

C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt

To find where your PowerShell command history is stored, use the following command:

(Get-PSReadlineOption).HistorySavePath

Or use the following command:

Get-ChildItem -Path "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine" | Format-Table -AutoSize

Show or Search the Contents of the PowerShell Command History File

Starting in Windows PowerShell 3.0, the default value of the $MaximumHistoryCount preference variable is 4096. You can view your command history by displaying the contents of the history file:

cat (Get-PSReadlineOption).HistorySavePath

You can also search through your PowerShell command history for specific commands or keywords using the Select-String cmdlet:

cat (Get-PSReadlineOption).HistorySavePath | Select-String <keyword>

Replace <keyword> with the keyword or command you're looking for.

Clear the Command History

If you need to clear your command history, you can delete the history file. Remember that you need to reopen PowerShell to see the effect:

Remove-Item (Get-PSReadlineOption).HistorySavePath

Alternatively, you can clear the current session's command history along with deleting the history file:

Remove-Item (Get-PSReadlineOption).HistorySavePath; Clear-History

Related Resources and Reference: