如何在 Windows 10 中查看以前 PowerShell 會話的命令歷史記錄

Jacki

有沒有辦法查看所有 PowerShell 會話的命令行歷史記錄? Windows PowerShell 允許您使用以下命令查看當前會話期間執行的每個命令Get-History命令。但有時這還不夠。在本教程中,我們將向您展示如何查看 Windows 10 中所有先前會話的完整命令歷史記錄。

如何在 Windows 10 中查看以前 PowerShell 會話的命令歷史記錄

推薦閱讀:Windows 11:如何在 PowerShell 或命令提示符中查看命令歷史記錄

  1. 為了使用PowerShell命令歷史記錄功能,您需要首先安裝PS閱讀線使用以下命令的模塊。
    Install-Module PSReadLine

    如果提示您安裝NuGet 提供程序, 類型並按 Enter 鍵。

  2. 接下來,鍵入以下命令以顯示保存 PowerShell 命令歷史記錄的文件的路徑。
    (Get-PSReadlineOption).HistorySavePath

    要在 PowerShell 控制台上查看完整詳細的命令歷史記錄,請運行以下命令:
    cat (Get-PSReadlineOption).HistorySavePath

  3. 要清除您曾經輸入過的所有 PowerShell 命令歷史記錄,請輸入以下命令:
    Remove-Item (Get-PSReadlineOption).HistorySavePath

    如果需要阻止 PowerShell 保存命令歷史記錄,請執行以下命令:
    Set-PSReadlineOption -HistorySaveStyle SaveNothing

    每當您想要配置 PowerShell 以再次跟踪所有執行的命令時,請運行以下命令:
    Set-PSReadlineOption -HistorySaveStyle SaveIncrementally

就是這樣!