about_History

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_History

簡短描述

描述如何在命令歷程記錄中取得並執行命令。

詳細描述

當您在命令提示字元輸入命令時,Windows PowerShell® 會在命令歷程記錄中儲存此命令。您可以使用歷程記錄中的命令做為您的工作記錄。而且,您可以從命令歷程記錄重新呼叫並執行命令。

歷程記錄 CMDLET

Windows PowerShell 有一組可管理命令歷程記錄的 Cmdlet。

          Cmdlet (Alias)       Description
          -------------------  ------------------------------------------
          Get-History (h)      Gets the command history.

          Invoke-History (r)   Runs a command in the command history.

          Add-History          Adds a command to the command history.

          Clear-History (clh)  Deletes commands from the command history.

適用於管理歷程記錄的鍵盤快速鍵

在 Windows PowerShell 主控台中,您可以使用下列快速鍵來管理命令歷程記錄。

若為其他主應用程式,請參閱產品文件。

          Use this key      To perform this action
          -------------     ----------------------------------------------
          UP ARROW          Displays the previous command.
 
          DOWN ARROW        Displays the next command.
 
          F7                Displays the command history. 
                            To hide the history, press ESC.
 
          F8                Finds a command. Type one or more characters,
                            and then press F8. For the next instance, 
                            press F8 again.

          F9                Find a command by history ID. Type the history
                            ID, and then press F9. To find the ID, press F7.

MAXIMUMHISTORYCOUNT

$MaximumHistoryCount 喜好設定變數可決定 Windows PowerShell 可在命令歷程記錄中儲存的命令數目上限。預設值為 4096,這表示 Windows PowerShell 可儲存 4096 個最新命令,但是您可以變更此變數的值。

例如,下列命令可將 $MaximumHistoryCount 降低為 100 個命令:

          $MaximumHistoryCount = 100

若要套用設定,請重新啟動 Windows PowerShell。

若要對您所有的 Windows PowerShell 工作階段儲存新變數值,請將指派陳述式加入至 Windows PowerShell 設定檔。如需設定檔的詳細資訊,請參閱 about_Profiles (https://go.microsoft.com/fwlink/?LinkID=113729)。

如需 $MaximumHistoryCount 喜好設定變數的詳細資訊,請參閱 about_Preference_Variables (https://go.microsoft.com/fwlink/?LinkID=113248)。

注意:

在 Windows PowerShell 2.0 中,$MaximumHistoryCount 喜好設定變數的預設值為 64。

歷程記錄中的命令順序

當命令完成執行時 (而不是在輸入命令時),命令就會加入至歷程記錄。如果命令需要一些時間才能完成,或者命令是在巢狀提示字元中執行,命令可能不會依序顯示在歷程記錄中。(只有在您結束提示字元層級時,在巢狀提示字元中執行的命令才會完成)。

另請參閱

about_Line_Editing

about_Preference_Variables

about_Profiles

about_Variables