適用対象: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0
about_Wildcards
Windows PowerShell® でワイルドカード文字を使用する方法について説明します。
ワイルドカード文字は 1 つ以上の文字を表します。ワイルドカード文字を使用して、コマンド内に単語パターンを作成できます。たとえば、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
1 つの単語パターンに複数のワイルドカード文字を含めることができます。たとえば、名前が "a" ~ "l" のいずれかの文字で始まるテキスト ファイルを検索するには、次のように入力します。
Get-ChildItem c:\techdocs\[a-l]*.txt
多くのコマンドレットで、パラメーター値にワイルドカード文字を使用できます。各コマンドレットのヘルプ トピックで、ワイルドカード文字を使用できるパラメーターがある場合は、詳細が説明されています。ワイルドカード文字を使用できるパラメーターでは、大文字と小文字が区別されません。
また、コマンドやスクリプト ブロックでも、プロパティの値を表す単語パターンを作成する場合などに、ワイルドカード文字を使用できます。たとえば、次のコマンドは、ServiceType プロパティの値に "Interactive" が含まれているサービスを取得します。
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