• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Wildcards

PowerShell Wildcards

30/07/2018 by Stephanos 2 Comments

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 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:

PowerShell Wildcards - Example 1 - Asterisk

 

[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:

PowerShell Wildcards - Example 2 - Asterisk

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:

PowerShell Wildcards - Example 3 - Asterisk

 

[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:

PowerShell Wildcards - Example 4 - Question Mark

 

[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:

PowerShell Wildcards - Example 5 - Square Brackets

 

[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:

PowerShell Wildcards - Example 6 - Multiple

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:

PowerShell Wildcards - Example 7 - Multiple

[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:

PowerShell Wildcards - Example 8 - Multiple

 

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:

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_Wildcards | Microsoft Docs
  • PowerShell Comparison Operators
  • Get-Service – Microsoft Docs
  • Where-Object – Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Wildcards
Article Name
PowerShell Wildcards
Description
PowerShell Wildcards. In this tutorial you will find information about PowerShell Wildcards and their use. Stephanos Constantinou Blog
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Comparison Operators, Get-Service, PowerShell Operators, Where-Object

Reader Interactions

Comments

  1. Joshua says

    02/08/2018 at 16:53

    Just found your blog a few days ago and I love it. Just a few helpful tips a a time so I don’t feel overwhelmed, but all good stuff that I need to remember.

    I was just needing to search for a specific pattern, but Regex was going to be more complicated that I needed. A ? was all I was needing but didn’t know I could do that in Powershell. Thanks!

    Reply
    • Stephanos says

      02/08/2018 at 17:08

      Hello Joshua,

      Thank you for your feedback and I am glad that I helped a bit. I hope to keep you interested and find my future posts useful too.

      Thanks
      Stephanos

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact