使用 Cmdlet
Cmdlet 是內建於殼層中的簡單、具備單一功能的命令列工具。Cmdlet 的使用方式,就像在使用傳統命令和公用程式的方式一樣。讓我們從在 Windows PowerShell 命令提示字元中輸入 Cmdlet 名稱開始說明。Windows PowerShell 命令並不區分大小寫,所以您可以輸入任何大小寫字元。
例如,您可以試試 Get-Date Cmdlet:
C:\PS> get-date
Thursday, November 10, 2005 4:43:50 PM
若要列出工作階段中的 Cmdlet,請使用 Get-Command Cmdlet 而不包含任何命令參數。
PS> get-command
CommandType Name Definition
----------- ---- ----------
Cmdlet Add-Content Add-Content [-Path] <String[...
Cmdlet Add-History Add-History [[-InputObject] ...
Cmdlet Add-Member Add-Member [-MemberType] <PS...
...
…
預設的 Get-Command 顯示包含三個欄位:CommandType、Name 和 Definition。當列出 Cmdlet 時,Definition 欄位會顯示該 Cmdlet 的語法。語法中的省略符號 (…) 表示資料已被截斷。
Get-Command Cmdlet 也會取得命令與非 Cmdlet 的命令元素,包括可用於 Windows PowerShell 的別名 (命令暱稱)、函數和執行檔。
下列命令透過 Get-Command 的 Name 參數,列出 Windows PowerShell 中的可用執行檔。
PS> get-command -name *.exe
CommandType Name Definition
----------- ---- ----------
Application 000StTHK.exe C:\WINDOWS\system32\000StTHK.exe
Application 00THotkey.exe C:\WINDOWS\system32\00THotkey.exe
Application accwiz.exe C:\WINDOWS\system32\accwiz.exe
...
當列出執行檔時,Definition 欄位會包含該執行檔的完整路徑。
接下來,請試試看其他一些 Cmdlet,例如,Get-Process、Get-Service、Get-EventLog 和 Get-Alias。
當您已經很習慣使用簡單的 "Get-" Cmdlet 時,請嘗試 Get-WmiObject。這個 Cmdlet 可以讓您檢視並變更遠端電腦的元件。例如,下列命令會取得 Server01 遠端電腦上 BIOS 的相關資訊:
get-wmiobject win32_bios -computername server01
若您需要任何 Cmdlet 的說明,請輸入:
get-help <cmdlet-name> -detailed
例如:
get-help get-alias -detailed