about_Parameters

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

主題

about_Parameters

簡短描述

描述如何在 Windows PowerShell 中使用命令參數。

詳細描述

大部分 Windows PowerShell 命令 (如 Cmdlet、函數和指令碼) 都會依賴參數,以便讓使用者選取選項或提供輸入。參數會遵循命令名稱並具有下列形式:

        -<parameter_name> <parameter_value>

參數的名稱前面會加上連字號 (-),以向 Windows PowerShell 表示連字號後面的字是參數名稱。某些參數不需要或不接受參數值。其他參數需要一個值,但不需要命令中的參數名稱。

參數的類型及這些參數的需求有所不同。若要尋找命令參數的相關資訊,請使用 Get-Help Cmdlet。例如,若要尋找 Get-Childitem Cmdlet 的參數相關資訊,請輸入:

        Get-Help Get-ChildItem

若要尋找指令碼參數的相關資訊,請使用指令碼檔案的完整路徑。例如:

        Get-Help $home\Documents\Scripts\Get-Function.ps1

Get-Help Cmdlet 會傳回有關命令的各種詳細資料,包括描述、命令語法、參數相關資訊,以及示範如何在命令中使用參數的範例。

您也可以使用 Get-Help Cmdlet 的參數來尋找特定參數的相關資訊。或者,您可以使用具有萬用字元 (*) 值的參數來尋找命令的所有參數相關資訊。例如,下列命令會取得 Get-Member Cmdlet 的所有參數相關資訊:

        Get-Help Get-Member -Parameter *

預設參數值

選擇性參數具有預設值,這是在命令中未指定此參數時所使用或假設的值。

例如,許多 Cmdlet 的 ComputerName 參數預設值是本機電腦的名稱。因此,除非指定了 ComputerName 參數,否則本機電腦名稱會使用於命令中。

若要尋找預設參數值,請參閱 Cmdlet 的說明主題。參數描述應包含預設值。

您也可以針對 Cmdlet 或進階函式的任何參數設定自訂預設值。如需設定自訂預設值的相關資訊,請參閱 about_Parameters_Default_Values。

參數屬性表

當您使用 Get-Help Cmdlet 的 Full、Parameter 或 Online 參數時,Get-Help 會顯示內含參數詳細資訊的參數屬性表。

此資訊包括使用參數所需了解的詳細資料。例如,Get-ChildItem Cmdlet 的說明主題包含有關其 Path 參數的下列詳細資料:

        -path <string[]>
            Specifies a path of one or more locations. Wildcard characters are
            permitted. The default location is the current directory (.).

        Required?                    false
        Position?                    1
        Default value                Current directory
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  true

參數資訊包括參數語法、參數描述和參數屬性。下列各節描述參數屬性。

必要參數?

此設定指出參數是否必要,也就是使用此 Cmdlet 的所有命令是否都必須包含此參數。當值為 "True" 且命令中遺漏此參數時,Windows PowerShell 會提示您輸入參數的值。

參數位置?

此設定會指出您是否可以提供參數的值,而不需在它的前面加上參數名稱。如果設定為 "0" 或 "named",則需要參數名稱。這類型的參數也稱為具名參數。具名參數可以列在 Cmdlet 名稱之後的任何位置。

如果 [參數位置?] 設定已設為 0 以外的整數,則不需要參數名稱。此類型的參數也稱為位置參數,而數字表示此參數必須出現在相對於其他位置參數的位置。如果您包含位置參數的參數名稱,此參數即可列在 Cmdlet 名稱之後的任何位置。

例如,Get-ChildItem Cmdlet 有 Path 和 Exclude 參數。Path 的 [參數位置?] 設定為 1,這表示它是位置參數。Exclude 的 [參數位置?] 設定為 0,這表示它是具名參數。

這表示 Path 不需要參數名稱,但其參數值必須是命令中第一個或唯一的未命名參數值。不過,因為 Exclude 參數為具名的參數,所以您可將它放在命令中的任何位置。

由於這兩個參數的 [參數位置?] 設定,您可以使用下列任何命令:

        Get-ChildItem -path c:\techdocs -exclude *.ppt
        Get-ChildItem c:\techdocs -exclude *.ppt
        Get-ChildItem -exclude *.ppt -path c:\techdocs
        Get-ChildItem -exclude *.ppt c:\techdocs

如果您要包含另一個位置參數,但不包含參數名稱,該參數就必須以 [參數位置?] 設定所指定的順序放置。

參數類型

此設定會指定參數值的 Microsoft .NET Framework 類型。例如,如果類型是 Int32,則參數值必須是整數。如果類型是字串,則參數值必須是字元字串。如果字串包含空格,此值必須括在引號內,或空格的前面必須加上逸出字元 (')。

預設值

此設定會指定參數將會採用的值 (如果未提供任何其他值的話)。例如,Path 參數的預設值通常是目前的目錄。必要參數一定沒有預設值。對於許多選擇性參數而言,因為參數未使用時並沒有作用,所以沒有預設值。

接受多個值?

此設定會指出參數是否接受多個參數值。當參數接受多個值時,您可以輸入以逗號分隔的清單做為命令中的參數值,或在變數中儲存以逗號分隔的清單 (陣列),然後將此變數指定為參數值。

例如,Get-Service Cmdlet 的 ServiceName 參數會接受多個值。下列兩個命令都有效:

        get-service -servicename winrm, netlogon


        $s = "winrm", "netlogon"
        get-service -servicename $s

接受管線輸入?

此設定指出您是否可以使用管線運算子 (|) 將值傳送至參數。

    
    Value                    Description
    -----                    -----------
    False                    Indicates that you cannot pipe a value to the 
                             parameter.


    True (by Value)          Indicates that you can pipe any value to the 
                             parameter, just so the value has the .NET 
                             Framework type specified for the parameter or the
                             value can be converted to the specified .NET 
                             Framework type.


                             When a parameter is "True (by Value)", Windows 
                             PowerShell tries to associate any piped values 
                             with that parameter before it tries other methods
                             to interpret the command.


    True (by Property Name)  Indicates that you can pipe a value to the 
                             parameter, but the .NET Framework type of the 
                             parameter must include a property with the same
                             name as the parameter.
 
                             For example, you can pipe a value to a Name 
                             parameter only when the value has a property 
                             called "Name".

接受萬用字元?

此設定指出參數的值是否可以包含萬用字元,以便讓參數值可以對應到目標容器中的多個現有項目。

一般參數

一般參數是您可以用於任何 Cmdlet 的參數。如需一般參數的詳細資訊,請參閱 about_CommonParameters

另請參閱

about_Command_syntax

about_Comment_Based_Help

about_Functions_Advanced

about_Parameters_Default_Values

about_Pipelines

about_Wildcards