about_Wildcards
Short description
Describes how to use wildcard characters in PowerShell.
Long description
Wildcard characters represent one or many characters. You can use them to
create word patterns in commands. Wildcard expressions are used with the
-like
operator or with any parameter that accepts wildcards.
For example, to match all the files in the C:\Techdocs
directory with a
.ppt
file name extension, type:
Get-ChildItem C:\Techdocs\*.ppt
In this case, the asterisk (*
) wildcard character represents any characters
that appear before the .ppt
file name extension.
Wildcard expressions are simpler than regular expressions. For more information, see about_Regular_Expressions.
PowerShell supports the following wildcard characters:
*
- Match zero or more charactersa*
matchesaA
,ag
, andApple
a*
doesn't matchbanana
?
- For strings, match one character in that position?n
matchesan
,in
, andon
?n
doesn't matchran
?
- For files and directories, match zero or one character in that position?.txt
matchesa.txt
andb.txt
?.txt
doesn't matchab.txt
[ ]
- Match a range of characters[a-l]ook
matchesbook
,cook
, andlook
[a-l]ook
doesn't matchtook
[ ]
- Match specific characters[bc]ook
matchesbook
andcook
[bc]ook
doesn't matchhook
`*
- Match any character as a literal (not a wildcard character)12`*4
matches12*4
12`*4
doesn't match1234
You can include multiple wildcard characters in the same word pattern. For example, to find text files with names that begin with the letters a through l, type:
Get-ChildItem C:\Techdocs\[a-l]*.txt
Note
Wildcard matching for filesystem items works differently than for strings. For more information, see the Remarks section of the DirectoryInfo.GetFiles(String, EnumerationOptions) method.
There may be cases where you want to match the literal character rather than
treat it as a wildcard character. In those cases you can use the backtick
(`
) character to escape the wildcard character so that it is compared
using the literal character value. For example, '*hello`?*'
matches strings
containing "hello?".
Many cmdlets accept wildcard characters in parameter values. The Help topic for each cmdlet describes which parameters accept wildcard characters. For parameters that accept wildcard characters, their use is case-insensitive.
You can use wildcard characters in commands and script blocks, such as to create a word pattern that represents property values. For example, the following command gets services in which the ServiceType property value includes Interactive.
Get-Service | Where-Object {$_.ServiceType -Like "*Interactive*"}
In the following example, the If
statement includes a condition that uses
wildcard characters to find property values. If the restore point's
Description includes PowerShell, the command adds the value of the
restore point's CreationTime property to a log file.
$p = Get-ComputerRestorePoint
foreach ($point in $p) {
if ($point.description -like "*PowerShell*") {
Add-Content -Path C:\TechDocs\RestoreLog.txt "$($point.CreationTime)"
}
}
See also
PowerShell