about_Wildcards

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

主題

about_Wildcards

簡短描述

描述如何使用 Windows PowerShell® 中的萬用字元。

詳細描述

萬用字元表示一或多個字元。您可以使用它們在命令中建立文字模式。例如,若要取得 C:\Techdocs 目錄中有 .ppt 副檔名的所有檔案,請輸入:

        Get-ChildItem c:\techdocs\*.ppt

在此情況下,星號 (*) 萬用字元表示 .ppt 副檔名之前出現的任何字元。

Windows PowerShell 支援下列萬用字元。

        Wildcard Description        Example  Match             No match
        -------- ------------------ -------- ----------------- --------
        *        Matches zero or    a*       A, ag, Apple      banana
                 more characters

        ?        Matches exactly    ?n       an, in, on        ran
                 one character in 
                 the specified 
                 position

        [ ]      Matches a range    [a-l]ook book, cook, look  took
                 of characters
 
        [ ]      Matches specified  [bc]ook  book, cook        hook
                 characters

您可以在相同的文字模式中包含多個萬用字元。例如,若要尋找名稱以字母 "a" 到 "l" 開頭的文字檔案,請輸入:

         Get-ChildItem c:\techdocs\[a-l]*.txt

許多 Cmdlet 在參數值中接受萬用字元。每個 Cmdlet 的 [說明] 主題會說明哪些參數 (若有的話) 允許萬用字元。對於接受萬用字元的參數,其使用不區分大小寫。

您也可以在命令和指令碼區塊中使用萬用字元,例如建立表示屬性值的文字模式。例如,下列命令會取得服務,其中 ServiceType 屬性值包含「互動」。

        Get-Service | Where-Object {$_.ServiceType -like "*Interactive*"}

在下列範例中,萬用字元是用來尋找 If 陳述式的條件中的屬性值。這個命令中,如果還原點的描述包含 "PowerShell",則命令會將還原點的 CreationTime 屬性值新增至記錄檔。

        $p = Get-ComputerRestorePoint
        foreach ($point in $p) 
          {if ($point.description -like "*PowerShell*") 
              {add-content -path C:\TechDocs\RestoreLog.txt "$($point.CreationTime)"}}
             

另請參閱

about_Language_Keywords

about_If

about_Script_Blocks