PowerShell's Clear-History doesn't clear history

Fopedush picture Fopedush · Nov 6, 2012 · Viewed 53.1k times · Source

Recently I had to run a command that unfortunately required me to type a password right on the command line.

Afterwards, I cleared my screen with "Clear", but also wanted to clear the command history so the offending command wouldn't show up in the session history. Unfortunately, the Clear-History cmdlet doesn't seem to actually do what its documentation claims - running Clear-History doesn't seem to have any impact on the session history whatsoever.

I can still see previous commands in the pop-up history menu, and scroll through old commands by pressing the up key. Here's a screengrab demonstrating the problem:

PowerShell clear history failure

I've verified with Get-Command that Clear-History is indeed executing the expected built-in PowerShell cmdlet.

I've tried a few variations, such as "Clear-History -count 10 -newest", all failing to show any effect. When I specify an exact history ID, such as "Clear-History -id 3", I receive an error like this:

Clear-History : Cannot locate history for Id 3.

Even if I can see command #3 on the screen.

Answer

Vimes picture Vimes · Apr 27, 2016

On Windows 10, the history and sensitive data show up again in future sessions, even after Alt+F7 and clear-history. It turns out the history is stored in a text file found at:

(Get-PSReadlineOption).HistorySavePath

I delete the offending line from that file or delete the whole file, and then end the current session or clear it via CB's answer