最近,我还得跑,不幸的是要求我在命令行上输入密码正确的命令。
后来,我清屏“清除”,但也想清除命令历史记录,以便有问题的命令不会在会话历史记录中显示。 不幸的是, 清除,历史 cmdlet的似乎并没有真正做到其文档索赔什么-运行清除,历史似乎没有对任何会话历史记录中的任何影响。
我仍然可以看到在弹出菜单的历史以前的命令,然后按向上键在旧命令滚动。 这里有一个screengrab演示问题:
我已经验证了获取命令是清除的历史记录被确实执行预期的内置PowerShell命令。
我已经尝试了一些变化,如“清除历史记录-Count 10 -Newest”,都未能表现出任何效果。 当我指定一个确切的历史记录ID,如“清除历史记录-id 3”,我收到这样的错误:
Clear-History : Cannot locate history for Id 3.
即使我能看到的命令#3的屏幕上。
为了补充CB的有用的答案。和JVimes的有用的答案 :
下面的高级功能捆绑的所有命令必须清除命令历史记录(无论是PowerShell的本身和控制台),既为doskey
风格和PSReadline
-模PowerShell控制台窗口:
注意:
<#
# .SYNOPSIS
# Clears the command history, including the saved-to-file history, if applicable.
#>
function Clear-SavedHistory {
[CmdletBinding(ConfirmImpact='High', SupportsShouldProcess)]
param(
)
# Debugging: For testing you can simulate not having PSReadline loaded with
# Remove-Module PSReadline -Force
$havePSReadline = ($null -ne (Get-Module -EA SilentlyContinue PSReadline))
Write-Verbose "PSReadline present: $havePSReadline"
$target = if ($havePSReadline) { "entire command history, including from previous sessions" } else { "command history" }
if (-not $pscmdlet.ShouldProcess($target))
{
return
}
if ($havePSReadline) {
Clear-Host
# Remove PSReadline's saved-history file.
if (Test-Path (Get-PSReadlineOption).HistorySavePath) {
# Abort, if the file for some reason cannot be removed.
Remove-Item -EA Stop (Get-PSReadlineOption).HistorySavePath
# To be safe, we recreate the file (empty).
$null = New-Item -Type File -Path (Get-PSReadlineOption).HistorySavePath
}
# Clear PowerShell's own history
Clear-History
# Clear PSReadline's *session* history.
# General caveat (doesn't apply here, because we're removing the saved-history file):
# * By default (-HistorySaveStyle SaveIncrementally), if you use
# [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory(), any sensitive
# commands *have already been saved to the history*, so they'll *reappear in the next session*.
# * Placing `Set-PSReadlineOption -HistorySaveStyle SaveAtExit` in your profile
# SHOULD help that, but as of PSReadline v1.2, this option is BROKEN (saves nothing).
[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()
} else { # Without PSReadline, we only have a *session* history.
Clear-Host
# Clear the doskey library's buffer, used pre-PSReadline.
# !! Unfortunately, this requires sending key combination Alt+F7.
# Thanks, https://stackoverflow.com/a/13257933/45375
$null = [system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[System.Windows.Forms.SendKeys]::Sendwait('%{F7 2}')
# Clear PowerShell's own history
Clear-History
}
}
在Windows 10,历史和敏感数据在未来的会议,即使Alt + F7,再次出现clear-history
。 在我找到的解决方案是:
Remove-Item (Get-PSReadlineOption).HistorySavePath
然后,结束当前会话或者通过清除CB的答案
要清除屏幕上显示历史(F7),你必须按Alt + F7。
这历史是由控制台缓冲区,而不是由PowerShell的,有它的历史被清除的管理Clear-History
cmdlet的。
脚本它,尝试:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.SendKeys]::Sendwait('%{F7 2}')