2012-11-06 536 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 

    } 

}