Clear-History
刪除 PowerShell 會話指令歷史中的條目。
語法
IDParameter (預設值)
Clear-History
[[-Id] <int[]>]
[[-Count] <int>]
[-Newest]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
CommandLineParameter
Clear-History
[[-Count] <int>]
[-CommandLine <string[]>]
[-Newest]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
Clear-History 刪除 PowerShell 會話中的指令歷史。 每個 PowerShell 會話都有自己的命令歷程記錄。 要顯示指令歷史,請使用 Get-History cmdlet。
預設情況下, Clear-History 會刪除 PowerShell 會話中整個指令歷史。 你可以用參數 Clear-History 來刪除選取的指令。
Clear-History 無法清除 PSReadLine 指令歷史檔案。 該 PSReadLine 模組儲存一個歷史檔案,包含每個 PowerShell 會話中的所有 PowerShell 指令。 從 PowerShell 提示音,使用鍵盤上的上下箭頭捲動指令歷史。 若要顯示指令歷史的設定, PSReadLine 請使用 Get-PSReadLineOption。
PSReadLine 隨 PowerShell 5.0 及以上版本出廠。 如需詳細資訊,請參閱 about_PSReadLine。
範例
範例 1:從 PowerShell 會話中刪除指令歷史
此指令會刪除 PowerShell 會話歷史中的所有指令。
Get-History
Id CommandLine
-- -----------
1 Set-Location .\Test
2 Update-Help
3 Set-Location C:\Test\Logs
4 Get-Location
Clear-History
Get-History
Id CommandLine
-- -----------
5 Clear-History
該 Get-History cmdlet 顯示 PowerShell 會話的歷史。
Clear-History 刪除整個指令歷史。
Get-History 顯示更新後的指令歷史,並確認先前的指令歷史已被刪除。
範例 2:刪除最新的指令
此指令使用 Count 和 Newest 參數來刪除 PowerShell 會話歷史中的最新指令。
Get-History
Id CommandLine
-- -----------
1 Set-Location C:\Test\
2 Get-Command Clear-History
3 Get-Command Clear-History -Syntax
4 Get-Command Clear-History -ShowCommandInfo
5 Get-Help Get-Alias
6 Get-Command Get-ChildItem -Syntax
7 Get-Help Clear-History
8 Set-Location C:\Test\Logs
9 Get-Help Get-Variable
10 Get-Help Get-ChildItem
Clear-History -Count 5 -Newest
Get-History
Id CommandLine
-- -----------
1 Set-Location C:\Test\
2 Get-Command Clear-History
3 Get-Command Clear-History -Syntax
4 Get-Command Clear-History -ShowCommandInfo
5 Get-Help Get-Alias
11 Clear-History -Count 5 -Newest
該 Get-History cmdlet 顯示 PowerShell 會話的歷史。
Clear-History 用來刪除指令歷史紀錄。
Count 參數指定刪除指令數量,包含指定的 ID。最新參數指定最新的指令會從歷史中清除。
Get-History 顯示更新後的指令歷史,並確認五個最新的指令已被刪除,ID 6 - ID 10。
範例 3:符合特定條件的刪除指令
此指令會刪除符合 CommandLine 參數定義特定條件的指令。
Get-History
Id CommandLine
-- -----------
1 Set-Location C:\Test\
2 Get-Command Clear-History
3 Get-Command Clear-History -Syntax
4 Get-Command Clear-History -ShowCommandInfo
5 Get-Help Get-Alias
6 Get-Command Get-ChildItem -Syntax
7 Get-Help Clear-History
Clear-History -CommandLine *Help*, *Syntax
Get-History
Id CommandLine
-- -----------
1 Set-Location C:\Test\
2 Get-Command Clear-History
4 Get-Command Clear-History -ShowCommandInfo
8 Clear-History -CommandLine *Help*, *Syntax
該 Get-History cmdlet 顯示 PowerShell 會話的歷史。
Clear-History 刪除指令歷史。
CommandLine 參數指定包含 Help 或以語法結尾的指令。
Get-History 顯示更新後的指令歷史,並確認指令 ID 3、 ID 5、 ID 6 和 ID 7 已被刪除。
範例 4:依 ID 編號刪除指令
此指令會使用 ID 刪除特定的歷史項目。若要刪除多個指令,請提交一份逗號分隔的 ID 編號清單。
Get-History
Id CommandLine
-- -----------
1 Set-Location C:\Test\
2 Get-History
3 Get-Help Get-Alias
4 Get-Command Clear-History
5 Get-Command Clear-History -Syntax
6 Get-Command Clear-History -ShowCommandInfo
Clear-History -Id 3, 5
Get-History
Id CommandLine
-- -----------
1 Set-Location C:\Test\
2 Get-History
4 Get-Command Clear-History
6 Get-Command Clear-History -ShowCommandInfo
7 Get-History
8 Clear-History -Id 3, 5
該 Get-History cmdlet 顯示 PowerShell 會話的歷史。
Clear-History 刪除指令歷史。
Id 參數指定要刪除哪些指令。
Get-History 顯示更新後的指令歷史,並確認 ID 3 與 ID 5 已被刪除。
範例 5:依 ID 編號與數量刪除指令
此指令使用 Id 和 Count 參數來刪除指令歷史紀錄。 指令會從指定的 ID 中依最新到最舊的順序倒序刪除。
Get-History
Id CommandLine
-- -----------
1 Set-Location C:\Test\
2 Get-Command Clear-History
3 Get-Command Clear-History -Syntax
4 Get-Command Clear-History -ShowCommandInfo
5 Get-Help Get-Alias
6 Get-Command Get-ChildItem -Syntax
7 Get-Help Clear-History
8 Set-Location C:\Test\Logs
9 Get-Help Get-Variable
10 Get-Help Get-ChildItem
Clear-History -Id 7 -Count 5
Get-History
Id CommandLine
-- -----------
1 Set-Location C:\Test\
2 Get-Command Clear-History
8 Set-Location C:\Test\Logs
9 Get-Help Get-Variable
10 Get-Help Get-ChildItem
11 Clear-History -Id 7 -Count 5
該 Get-History cmdlet 顯示 PowerShell 會話的歷史。
Clear-History 刪除指令歷史。
Id 參數指定以 ID 7 開頭。
Count 參數指定刪除包含指定 ID 的五個指令。Get-History顯示更新後的指令歷史,並確認有五個指令被刪除,ID 3 - ID 7。
參數
-CommandLine
刪除 PowerShell 會話中的指令歷史。 字串必須完全匹配,或使用通配符來匹配 PowerShell 會話歷史中顯示的 Get-History指令。 如果你輸入了多個字串, Clear-History 刪除與任何字串相符的指令。
CommandLine 參數可與 Count 一起使用。
對於有空格的字串,請使用單引號。 如需詳細資訊,請參閱 about_Quoting_Rules。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
CommandLineParameter
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Confirm
執行指令長前 Clear-History 會提示你確認。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 參見 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Count
指定刪除的歷史條目 Clear-History 數量。 指令依序刪除,從歷史上最早的條目開始。
計 數 和 ID 參數可以一起使用。
Count 參數指定刪除指令數量,包含指定的 ID。從指定的 ID 開始,指令會以反向順序刪除。 例如,若 ID 是 30,計 數 是 10,則 Clear-History 刪除第 21 到 30 的項目。
Count 參數與 CommandLine 參數可同時使用。 Count 指定刪除與 CommandLine 參數值相符的指令數量。 指令會依序刪除。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 1 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Id
指定刪除的指令歷史 IDClear-History。 要顯示 ID 編號,請使用 Get-History cmdlet。
ID 編號是連續的,指令會在整個 PowerShell 會話中保持 ID 編號。
Id 參數可與 Count 及 Newest 一起使用。
參數屬性
| 類型: | Int32[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
IDParameter
| Position: | 0 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Newest
當使用 最新 參數時,會 Clear-History 刪除歷史中最新的條目。 預設情況下,會 Clear-History 刪除歷史上最早的條目。
最新的參數可與 ID 和 Count 一起使用。
Count 參數指定刪除指令數量,包含指定的 ID。從指定的 ID 開始,指令依序被刪除。 例如,若 ID 是 30,Count 是 10,則Clear-History刪除第 30 到 39 的項目。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-WhatIf
顯示如果 Clear-History 指令執行會發生什麼事。 指令未執行。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 威斯康辛 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
None
您不能將物件透過管道傳送到此 Cmdlet。
輸出
None
此 Cmdlet 不會傳回任何輸出。
備註
PowerShell 包含下列 Clear-History的別名:
- 所有平臺:
clhy
PowerShell 會話歷史是 PowerShell 會話中輸入的指令清單。 你可以查看歷史紀錄、新增或刪除指令,並從歷史中執行指令。 如需詳細資訊,請參閱 about_History。
會話歷程記錄會與 PSReadLine 模組所維護的歷程記錄分開管理。 這兩個歷程記錄可在載入 PSReadLine 的會話中使用。 此 Cmdlet 僅適用於會話歷程記錄。 欲了解更多資訊,請參閱 about_PSReadLine。