Get-Command 是 PowerShell 探索可用命令(Cmdlet、Function、Alias、Script、Application 等)的主要工具。本文整理官方說明文件重點,提供語法、參數、範例與進階用法速查。
Get-Command [[-Name] <String[]>] [[-ArgumentList] <Object[]>] [-CommandType {Alias|Function|Filter|Cmdlet|ExternalScript|Application|Script|All}] [-Module <String[]>] [-TotalCount <Int32>] [-Syntax] [<CommonParameters>]
Get-Command [[-ArgumentList] <Object[]>] [-Verb <String[]>] [-Noun <String[]>] [-Module <String[]>] [-TotalCount <Int32>] [-Syntax] [<CommonParameters>]
| 參數 | 說明 | 範例 |
|---|---|---|
-Name | 指定命令名稱(支援萬用字元) | Get-Command Get-* |
-Verb | 依動詞篩選(如 Get、Set、New) | Get-Command -Verb Set |
-Noun | 依名詞篩選(如 Process、Service、Item) | Get-Command -Noun Process |
-CommandType | 限定命令類型 | Get-Command -CommandType Function |
-Module | 僅列出指定模組匯出的命令 | Get-Command -Module Microsoft.PowerShell.Security |
-ArgumentList | 檢查命令搭配特定參數時的語法(動態參數) | Get-Command Get-ChildItem -ArgumentList cert: |
-Syntax | 僅顯示語法資訊 | Get-Command Get-Process -Syntax |
-TotalCount | 限制回傳命令數量 | Get-Command -TotalCount 10 |
# 1. 列出所有 Cmdlet
Get-Command
# 2. 找出所有動詞為 Set 的命令,以清單顯示完整語法
Get-Command -Verb Set | Format-List
# 3. 依名詞分組顯示所有命令,方便按功能尋找
Get-Command | Sort-Object Noun | Format-Table -GroupBy Noun
# 4. 查看特定模組提供的命令
Get-Command -Module Microsoft.PowerShell.Security
# 5. 檢查 Get-ChildItem 在憑證提供者下的動態參數
Get-Command Get-ChildItem -ArgumentList cert: | Format-List
# 6. 僅取得動態參數物件
(Get-Command Get-ChildItem -ArgumentList cert:).ParameterSets[0].Parameters | Where-Object { $_.IsDynamic }
# 7. 列出所有類型命令(含 Alias、Function、Script、Application)
Get-Command *
gcm,輸入更快速。Sort-Object 自訂排序。-ArgumentList 可檢視提供者(如 FileSystem、Registry、Certificate)動態注入的參數。CommandInfo 物件,可進一步管線至 Format-List * 查看所有屬性。Get-Help:取得詳細說明文件Get-Member:檢視物件成員Get-PSDrive:列出提供者磁碟機about_Command_Syntax:語法說明主題