PowerShell's Clear-History doesn't clear history

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

PowerShell's Clear-History doesn't clear history

Message par ForumBot »

PowerShell's Clear-History doesn't clear history
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: PowerShell's Clear-History doesn't clear history

Message par ForumBot »

**tl;dr**

-
There are ***two* histories to clear**:

- PowerShell's own, invariably in-session-only history (use `Clear-History` to clear it)

- Additionally, in consoles (terminals), that of the `PSReadLine` module that is used for command-line editing by default in PowerShell v5+, which provides both an *in-session* and a *persistent* history (to clear the in-session history only, use `[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()`)

- Alt-F7 in PSReadLine v1.2+ performs *both* calls for you, but note that the *persistent* history is unaffected (see next point).

-
In **versions 1.2+ of `PSReadLine`** (verify with `Get-Module PSReadLine`) **pressing Alt+F7 performs *both* calls for you**, and therefore **fully clears the *in-session* history**.

-
However, **it does *not* clear the *persistent* history that has accumulated up to this point**, so even the cleared session's history will *resurface* in future sessions by default.

-
**To also clear the *persistent* (saved-to-a-file) history**, you currently have to ***manually* delete the underlying file** (`Remove-Item (Get-PSReadlineOption).HistorySavePath`), as discussed below, and as wrapped by the `Clear-SavedHistory` function in the bottom section.

-
[GitHub issue #25933](https://github.com/PowerShell/PowerShell/issues/25933) is a proposal to implement a new cmdlet that makes deleting the persistent history (too) easier.

To complement [CB.'s helpful answer](https://stackoverflow.com/a/13257933/45375) and [JVimes's helpful answer](https://stackoverflow.com/a/36900056/45375):

-
**PowerShell's own history mechanism (`Get-History`, `Clear-History`) is *host-independent***, which is why - somewhat unexpectedly - **you also need to clear the *hosts*'s command history *separately***.

-
As for the **console host's own history feature**:

-
**`doskey`-style history feature**, before module `PSReadline` shipped with PowerShell (see below):

- There is **no *saved* history** - a history is kept onl

*(Réponse tronquée)*
Répondre

Revenir à « PowerShell »