Problem Clearing PowerShell Console History Commands

Issue:

I was surprise to see sensitive information was cached in Windows PowerShell console history when I pressed up arrow key on the keyboard. I tried to clear out with command “Clear-History”, ALT+F7, and[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory(). None of them work after I re-open the PowerShell console. What to do?

Environment:

Windows 10 with Windows PowerShell

Resolution:

Option 1:

If you want to keep certain non-sensitive commands on your history and skip storing any sensitive commands. Execute below command before start executing sensitive command. (Note: It will reset to default settings when you restart your PowerShell console)

PS C:\> Set-PSReadlineOption -HistorySaveStyle SaveNothing

Step-by-step instructions:-

  1. Start Windows PowerShell console
  2. Before executing sensitive command, enter command: Set-PSReadlineOption -HistorySaveStyle SaveNothing
  3. You can now executing sensitive command
  4. To verify if sensitive command was stored, restart Windows PowerShell and type Get-History or press arrow key up to see command history

Option 2:

If you want to clear out all historical entries, execute below command.

PS P:\> Remove-Item (Get-PSReadlineOption).HistorySavePath

Step-by-step instructions:-

  1. Start Windows PowerShell console
  2. Enter command: Remove-Item (Get-PSReadlineOption).HistorySavePath
  3. Restart Windows PowerShell console
  4. Type Get-History or press arrow key up to see command history

Notes:

To see default History Save Path value:

PS C:\> (Get-PSReadlineOption).HistorySavePath

C:\Users\[your-username]\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt

To see default History Save Style value:

PS C:\> (Get-PSReadlineOption).HistorySaveStyle

SaveIncrementally

To get PowerShell version:

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.16299.492
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.492
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Reference:

Set-PSReadline​Option: https://docs.microsoft.com/en-us/powershell/module/psreadline/set-psreadlineoption?view=powershell-6