2012-11-06 423 views
17

最近我不得不运行一个命令,不幸的是我需要在命令行上输入密码。之后,我用“Clear”清除了我的屏幕,但也想清除命令历史记录,以便在会话历史记录中不显示违规命令。不幸的是,明确的历史cmdlet似乎并没有真正做它的文档声明 - 运行明确的历史记录似乎没有任何影响会话历史。我仍然可以在弹出的历史记录菜单中看到以前的命令,并通过按向上键滚动浏览旧命令。这里有一个screengrab演示问题:Powershell清除历史不会清除历史记录

Powershell clear history failure

我和GET-命令验证清除的历史记录被确实执行预期的内置PowerShell命令。

我已经尝试了一些变化,如“清除历史记录-Count 10 -Newest”,都未能表现出任何效果。当我指定一个确切的历史记录ID,如“清除历史记录-id 3”,我收到这样的错误:

Clear-History : Cannot locate history for Id 3. 

即使我能看到的命令#3的屏幕上。任何意见或想法将不胜感激。

+0

只是一个侧面说明,但你实际上用'清除-History'清除历史记录,是你用'GET-History'看到一个。 –

回答

22

要清除屏幕显示历史记录(F7),您必须按ALT + F7。

这历史是由控制台缓冲区管理不是通过clear-history cmdlet都具有自己的历史可清除PowerShell的

其脚本的尝试:

[system.reflection.assembly]::loadwithpartialname("System.Windows.Forms") 
[System.Windows.Forms.SendKeys]::Sendwait('%{F7 2}') 
+0

野生。任何想法,如果该功能可以复制编程方式,所以我可以别名清除历史做这两个? – Fopedush

+0

@Fopedush编辑我的答案为脚本添加代码 –

+0

工程就像一个魅力。谢谢。 – Fopedush

24

在Windows 10,历史和敏感数据在未来的会议中再次出现,即使在Alt + F7和clear-history之后。该solution I found是:

Remove-Item (Get-PSReadlineOption).HistorySavePath 

然后,结束当前会话或者通过CB's answer

+0

仅供懒惰管理员使用,保存命令的文本文件的默认路径为: C:\ Users \ [用户名] \ AppData \ Roaming \ Microsoft \ Windows \ PowerShell \ PSReadline 该文件被重新创建控制台启动的时间。 我竟然运行了Remove-Module PSReadline -Force,但是当我再次重新启动控制台时,模块被重新添加(并且重新创建历史文件)。是否有一些超级模块删除功能? –

+1

也许当他们发布模块w/PowerShell时,他们做得太好导入它的工作。如果永久历史记录是你想要禁用的(不是所有其他psreadline [好东西](https://github.com/lzybkr/PSReadLine)),它[看起来像](https://github.com/lzybkr/ PSReadLine/issues/163)的方式是将'Set-PSReadlineOption -HistorySaveStyle SaveNothing'添加到您的配置文件。为了移除这个模块,现在可能把这个命令放到你的配置文件中会“足够好”。 – Vimes

+0

好的提示,但是为了清除_current_ session的历史记录,使用'[Microsoft.PowerShell.PSConsoleReadLine] :: ClearHistory()'是更安全的(另外:发送Alt + F7实际上在'PSReadline' v1中根本不起作用.1,这是Windows 10附带的版本(Alt + F7在v1.2中引入) – mklement0

7

清除它为了补充CB.'s helpful answerJVimes's helpful answer

  • PowerShell的自己的历史机制(Get-HistoryClear-History)是主机无关,这就是为什么 - 有些人帽子意外 - 还需要清除主机的命令历史记录分别

  • 对于控制台宿主自身的历史特点

    • doskey风格的历史特征,附带的PowerShell模块PSReadline前(见下文):

      • 已保存历史 - 历史y只保留在本届会议期间。
      • Alt + F7必须用于清除控制台的历史,没有(明显)编程的方式来做到这一点(在你可以使用doskey /reinstall一个cmd.exe控制台窗口,但是,这并不在PS工作)。
      • CB.'s answer向您展示如何模拟此键盘组合;请记住:除了Clear-History之外,必须使用
    • PSReadline模块自带的PowerShell V5在Windows 10,也将与Windows Server 2016出货;它使用更复杂的功能取代doskey风格的行编辑和命令历史功能;还可以使用PowerShell Gallery(PSv3和PSv4必须首先安装PowerShellGet)对其旧版Windows版本/ PS版本(> = v3)进行改进。

      • 命令历史是现在保存在会话,在文件
        (Get-PSReadlineOption).HistorySavePath
      • [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()可以用来清除当前会议的历史(注意,V1.2 +还支持Alt + F7当前历史的互动结算
        • CAVEAT:随着PSReadline的默认历史保存样式,SaveIncrementally,任何敏感命令已经保存到您拨打电话[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()将重新出现在接下来会话
        • 来处理这个问题的唯一办法就是删除保存的历史文件,这表现在JVimes's answer,然而,总是抹了整个历史
        • IF您建立个人资料拨打Set-PSReadlineOption -HistorySaveStyle SaveAtExit每一个会话开始时间 - 设置apparenly没有“坚持”本身 - 你应该能够摆脱只调用[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()(除Clear-History)而不必删除已保存的历史记录文件,在这种情况下,您将不会丢失以前会话中保存的历史记录。 但是,因为v1.2,SaveAtExit是BROKEN ALTOGETHER - 根本没有历史记录;看到https://github.com/lzybkr/PSReadLine/issues/262

以下先进的功能捆绑的所有命令必须清除命令历史记录(无论是PowerShell的本身和控制台),既为doskey风格和PSReadline -module PowerShell控制台窗口:

注意:

  • 因为它是(目前)唯一安全的选择,PSReadline的保存的历史文件也被删除,这意味着整个历史,包括从以前的会议上被清除

  • 因此,默认显示确认提示。

<# 
# .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 

    } 

}