You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Question/Request: Is it possible to remove an item from the history? #1778
The [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory method makes it really easy to add a command to the history. Is there a corresponding method to remove a command from the history?
I would like to write a function to bind to a key that would recall the last command from the history so I can correct it. Sort of like an UndoLastCommand function. I would use this after entering a command with a misspelling or other syntax problem. Such a command has no useful purpose in the history (I would never want to execute it again).
Hopefully this description makes sense. Is such a thing possible today? Or would this require some coding within the PSReadLine module before I could write a PowerShell script to do this?
Not exactly - you can edit the history file, but I think that would only take effect in new instances of PowerShell.
This wouldn't be too hard to implement in memory, but deleting from the history file gets a bit tricky because of how shared history works - it relies on knowing the length of the history file and knowing that a new command would start at the next offset beyond that length.
Thanks Ben Allred (@benallred) for sharing that snippet. That is a very close approximation of the feature I wish was available out of the box. Ideally it would affect the current session, but it is much better than nothing.
FYI, the extra new lines is a quirk of the Set-Content command and can be avoided by changing it to
Clear-History is for the wrong history. This post is not talking about the PowerShell session history, but the PSReadLine history that's saved to the (Get-PSReadLineOption).HistorySavePath settting. Clear-History doesn't do anything with that history.
Activity
lzybkr commentedon Aug 28, 2020
Not exactly - you can edit the history file, but I think that would only take effect in new instances of PowerShell.
This wouldn't be too hard to implement in memory, but deleting from the history file gets a bit tricky because of how shared history works - it relies on knowing the length of the history file and knowing that a new command would start at the next offset beyond that length.
benallred commentedon Apr 27, 2022
I'm using this to [kind of] remove an item from the history: https://github.com/benallred/configs/blob/f8814951e66d33feb4ed045b456a2ca60bd46658/powershell/PSReadLine.ps1#L67-L82
It's not perfect, due to the reasons lzybkr mentioned:
camp-007 commentedon Jul 14, 2022
Thanks Ben Allred (@benallred) for sharing that snippet. That is a very close approximation of the feature I wish was available out of the box. Ideally it would affect the current session, but it is much better than nothing.
FYI, the extra new lines is a quirk of the
Set-Contentcommand and can be avoided by changing it totygliu commentedon Sep 15, 2023
Delete commands by Id number:
Clear-History -Id 3, 5
Clear-History doc
DJackman123 commentedon Sep 15, 2023
Clear-History is for the wrong history. This post is not talking about the PowerShell session history, but the PSReadLine history that's saved to the (Get-PSReadLineOption).HistorySavePath settting. Clear-History doesn't do anything with that history.
mikesigs commentedon Jun 24, 2024
I added this to my profile to help remove items from both history sets.
gwgaston commentedon Jul 30, 2024
Was hoping you guys had learned a few things from Rex Conn. :)
miaLDN commentedon Jan 19, 2025
Mike Sigsworth (@mikesigs) thank you :)
I've just added it to my profile also! I wanted to remove some auto-complete matches that were wrong/useless :)