最近,我不得不运行一个命令,不幸的是,该命令要求我在命令行中直接输入密码。
之后,我用" clear“清除了屏幕,但也想清除命令历史记录,这样有问题的命令就不会出现在会话历史记录中。不幸的是,Clear-History cmdlet似乎并没有像它的文档所宣称的那样做-运行Clear-History似乎对会话历史没有任何影响。
我仍然可以在弹出的历史菜单中看到以前的命令,并通过按up键滚动浏览旧的命令。以下是演示该问题的截图:
我已经向Get-Command验证了Clear-History确实在执行预期的内置PowerShell cmdlet。
我尝试了一些变体,比如"Clear-History -count 10 -newest",都没有显示出任何效果。当我指定一个确切的历史ID时,比如"Clear-History -id 3",我收到一个类似这样的错误:
Clear-History : Cannot locate history for Id 3.
即使我能在屏幕上看到命令#3。
发布于 2022-01-31 10:02:32
通过来自@mklement0的the best answer,我得到了下一个函数,它被放在我的$PROFILE
中
我真的不关心任何其他会话,我只想清除那段愚蠢的历史,仅此而已。
Cmdlet名称Clear-History
尽可能地混淆。
# it's a default alias for Get-History cmdlet
Remove-Alias history
# Usage: history - just print the history, same as call Get-History
# Usage: history -c - really clears the history
function history {
param (
# Clears history
[Parameter()]
[Alias("c")]
[Switch]
$Clear
)
if ($Clear){
Clear-History
[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()
return
}
Get-History
}
https://stackoverflow.com/questions/13257775
复制相似问题