上一節主要介紹Powershell可發現,面向對象,一致性等特性,以及Powershell命令是基於.Net對象等重要概念,以及Powershell命令的命名規範,詳細內容點擊這裏。shell
這一節的Powershell基礎知識主要包含如下知識點瀏覽器
獲取命令的摘要信息。網絡
獲取命令的幫助信息。ide
總結。函數
獲取命令的摘要信息學習
Powershell命令 Get-Command 可檢索當前shell中全部可用的命令名稱。在Powershell提示符輸入 Get-Command ,輸出的內容相似如下內容(如下只寫出輸出的結果的部份內容)。
spa
PS C:\Documents and Settings\Administrator> Get-Command CommandType Name Definition ----------- ---- ---------- Alias % ForEach-Object Alias ? Where-Object Function A: Set-Location A: Alias ac Add-Content Cmdlet Add-Computer Add-Computer [-DomainName] <String> [-Credential... Cmdlet Add-Content Add-Content [-Path] <String[]> [-Value] <Object[... Cmdlet Add-History Add-History [[-InputObject] <PSObject[]>] [-Pass... Cmdlet Add-Member Add-Member [-MemberType] <PSMemberTypes> [-Name]...
在 Get-Command 命令的輸出中,全部定義都以省略號 (...) 結尾,表示 PowerShell 沒法在可用空間內顯示全部內容。在顯示輸出時,PowerShell 會將輸出格式設置爲文本,而後對其進行排列,以使數據整齊地顯示在窗口中。在後續的文章中會對命令輸出的格式化作詳細說明。對象
Get-Command cmdlet 有一個 Syntax 參數,使用該參數,能夠僅檢索每一個 cmdlet 的語法。輸入 Get-Command -Syntax 命令能夠顯示完整的輸出:blog
PS C:\Documents and Settings\Administrator> Get-Command -Syntax ForEach-Object Where-Object A: Add-Content Add-Computer [-DomainName] <String> [-Credential <PSCredential>] [-OUPath <String>] [-PassThru] [-Server <String>] [-UnSecure] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <Stri ng>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]Add-Computer [-WorkGroupName] <String> [-Credential <PSCredential>] [-PassThru] [-Verbose] [-Debug] [-ErrorAction <Acti onPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable<String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm] Add-Content [-Path] <String[]> [-Value] <Object[]> [-PassThru] [-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Credential <PSCredential>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAction <Act ionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm] [-UseTransaction] [-Encoding <FileSystemCmdletProviderEncoding>] Add-Content [-LiteralPath] <String[]> [-Value] <Object[]> [-PassThru] [-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Credential <PSCredential>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningActi on <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm] [-UseTransaction] [-Encoding <FileSystemCmdletProviderEncoding>]
須要注意的是,Get-Command 命令僅列出當前 shell 中的 cmdlet,並非Powershell中的全部可用的命令。別名,函數和腳本也是Powershell命令,外部程序也歸類與命令。經過輸入如下命令,能夠返回全部可調用項的列表:ip
PS> Get-Command *
因爲此列表包括搜索路徑中的外部文件,所以它可能包含數千個項目。實際上,僅查看精簡的命令集更爲有用。若要查找其餘類型的本機命令,可使用 Get-Command cmdlet 的 CommandType 參數。儘管咱們還沒有介紹其餘這些命令類型,但若是知道某類命令的 CommandType 名稱,您仍然能夠顯示這些命令類型。
注意:命令中的星號(*) 是通配符,表示「匹配一個或多個任意字符」,如輸入Get-Command *ervice* 列出全部包含"ervice"的命令。
若要顯示特殊命令類別的別名(做標準命令名稱的替代名稱),可輸入如下命令:
PS> Get-Command -CommandType Alias
若要顯示全部 Windows PowerShell 函數,請輸入如下命令:
PS> Get-Command -CommandType Function
若要顯示 PowerShell 搜索路徑中的外部腳本,請輸入如下命令:
PS> Get-Command -CommandType ExternalScript
獲取命令的幫助信息
獲取cmdlet幫助信息
要獲取有關 PowerShell cmdlet 的幫助,請使用 Get-Help cmdlet。例如,要獲取Get-ChildItem的幫助,請輸入:
get-help get-childitem
或
get-childitem -?
固然能夠獲取Get-Help命令自己的幫助,例如:
get-help get-help
若要在會話中獲取全部 cmdlet 幫助主題的列表,請鍵入:
get-help -category cmdlet
若要每次顯示每一個幫助主題的一頁,請使用 help 函數或其別名 man。例如,若要顯示 Get-ChildItem cmdlet 的幫助,請鍵入
man get-childitem
或
help get-childitem
若要顯示有關 cmdlet、函數或腳本的詳細信息,包括其參數說明和使用示例,請使用 Get-Help cmdlet 的 Detailed 參數。例如,若要獲取有關 Get-ChildItem cmdlet 的詳細信息,請鍵入:
get-help get-childitem -detailed #顯示get-childitem的詳細信息 get-help get-childitem -full #顯示get-childitem幫助主題的所有內容 get-help get-childitem -parameter * #顯示get-childitem參數的詳細幫助 get-help get-childitem -examples #顯示get0childitem的幫助中的示例
注:Powershell中,#用於註釋,相似於Java或C#中的"//"。
獲取概念性幫助。Powershell經過 Get-Help about_* 命令獲取概念性的幫助。
獲取有關提供程序的幫助
Powershell能夠獲取提供程序的幫助信息。如要獲取Registry 提供程序的幫助,請輸入:
get-help registry
如要在會話中獲取全部提供程序幫助主題的列表,請輸入
get-help -category provider
Get-Help 的各個參數(如 Detailed、Parameter 和 Examples)對提供程序幫助主題的顯示沒有影響。
獲取連網幫助
若是計算機已經鏈接到網絡,查看幫助的最好方式是查看網上幫助主題,在線幫助主題更容易提供最新的內容。
若要使用 Get-Help cmdlet 的 Online 參數,請使用如下命令格式。
get-help <command-name> -online
若是該命令提供了幫助主題的聯機版本,則它將在默認瀏覽器中打開。
總結
經過學習本節,應當掌握如下內容。
能夠查看當前shell中全部可用的命令及Powershell中全部可用的命令。
對於任何一條命令會查看其摘要信息,語法信息及指定類型命令的檢索。
能夠獲取指定命令的幫助信息,包括在線幫助信息。
知道Powershell中"#"是用於內容的註釋。