PowerShell Wildcards

In this tutorial we will see about PowerShell Wildcards. Wildcards can be used in cases you want to match multiple characters. Wildcards are used to create patterns in your commands. For example when you are trying to filter results wildcards can be useful by creating a pattern of characters to match. Below is the list of wildcards that are supported in PowerShell.

Wildcards List

* – Matches zero or more characters

– Matches zero or more characters ? – Matches only one characters in a specific position.

– Matches only one characters in a specific position. [ ] – Matches the specific characters enclosed in the square brackets or a range of characters.

You are able to use a specific wildcard multiple times within the pattern in order to broaden or narrow you results.

Lets see below some examples for their use.

Asterisk – *

As we have mentioned above, the * character matches zero or more characters.

Example 1

In our first example we will retrieve all services that their name start with “net” and can be followed by anything.

Code:

Get-Service | Where-Object {$_.Name -like "net*"}

Output:

[adinserter name=”In Article”]

Example 2

In this example we will use * two times in our pattern. By using asterisk before and after the string we will look for any characters if there are any, before or after the string. You can compare the results from our first example to see the difference.

Code:

Get-Service | Where-Object {$_.Name -like "*net*"}

Output:

Example 3

for this example I have added * one more time. I have replaced “e” with * . Now the results that we will get will include any service that includes the letters “n” and “t” in the specific order. Remember that * can match zero or more characters, so it can be anything or nothing at the * position. See the code and output below to better understand.

Code:

Get-Service | Where-Object {$_.Name -like "*n*t*"}

Output:

[adinserter name=”In Article”]

Question Mark – ?

As we have mentioned above, ? matches only one character at the specific position. It can also be used multiple times to create our pattern of characters. Let’s see the examples below on how we are able to use it.

Example 4

In this example we can see that ? always tries to find a character at the specific position. It cannot be zero. You need to have the exact number of question marks in order to find matches. See the commands used below and their out.

Code:

Get-Service | Where-Object {$_.Name -like "?logon"} Get-Service | Where-Object {$_.Name -like "??logon"} Get-Service | Where-Object {$_.Name -like "???logon"} Get-Service | Where-Object {$_.Name -like "????logon"} Get-Service | Where-Object {$_.Name -like "???l?g?n"}

Output:

[adinserter name=”In Article”]

Square Brackets – [ ]

As we have mentioned above in [ ] you can enclose a range of characters that you want to match or provide the exact characters to match. The range on the characters that you include, apply only to the specific location. Let’s see the example below.

Example 5

Code:

Get-Service | Where-Object {$_.Name -like "[a-z]logon"} Get-Service | Where-Object {$_.Name -like "[a-z][a-f]logon"} Get-Service | Where-Object {$_.Name -like "[a-z][a-f][p-z]logon"} Get-Service | Where-Object {$_.Name -like "[a-z][a-f][c-z]logon"} Get-Service | Where-Object {$_.Name -like "[ns]e[ct]logon"}

Output:

[adinserter name=”In Article”]

Multiple wildcards

We are also able to combine multiple or all of the wildcards in order to get our results. Check the examples below.

Example 6

Code:

Get-Service | Where-Object {$_.Name -like "*n?t*"}

Output:

The pattern above provides us with matches that there is anything in front of “n”, a character in between “n” and “t” and anything after “t”. Unlike the result in Example 3, we defined that we need to have one character in between “n” and “t”. Any results with more characters in between or no character at all, are not included.

Example 7

Code:

Get-Service | Where-Object {$_.Name -like "*n[a-d]t*"} Get-Service | Where-Object {$_.Name -like "*n[a-e]t*"} Get-Service | Where-Object {$_.Name -like "*n[ae]t*"}

Output:

[adinserter name=”In Article”]

Example 8

In this example we are using all wildcards in the same command to define our pattern. The pattern’s points are below:

zero or many characters in front of “ne”

“ne” is followed by a character that is in the range of “s” to “z”

the characters from the range above is followed by any character and the by “o”.

“o” is followed by zero or many characters.

Code:

Get-Service | Where-Object {$_.Name -like "*ne[s-z]?o*"}

Output:

I hope the tutorial about PowerShell Wildcards is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

[adinserter name=”Matched-Content”]