Working with Different PowerShell Wildcard Expressions

Image of sunglasses resting on a playing card.
Win every PowerShell game with wildcards!
SOURCE: PxHere

One of PowerShell’s more commonly encountered tasks involves checking strings to see if they match a predetermined value. You often use the same process to query databases using MSQL from a terminal to retrieve all database entries that match your query. Sometimes you need to know the entire string, like the name of a file, but you only have the prefix or suffix. You have different ways to accomplish these tasks, but using a PowerShell wildcard can make the process easier. 

For those new to the term “matching”, it refers to querying data stored in a database or dataset to see if it matches your query item. Usually, it’s unlikely you know what data you’re looking for, so you use PowerShell wildcards to represent missing parts of your query. PowerShell wildcard matching benefits are enormous, as it often allows you to find or select the data you need. In this article, I’ll show you several tricks for finding strings with a PowerShell wildcard. First, I’ll take you through using an asterisk PowerShell wildcard. 

Asterisk PowerShell Wildcard

The asterisks are the most commonly used PowerShell wildcard, which can return any number of characters. For example, if we were to type Get-Process Explorer | Select-Object *, then the * at the end of the command would tell PowerShell to display all of the columns of information related to the process named Explorer.

When it comes to pattern matching, like searching a string for certain character combinations, an asterisk means the same thing. Its presence means that anything is a match. To show you what I mean, check out the two lines of code below:

$Data=”ABCDEF”

If ($Data -Like ‘*’) {Write-Host ‘Match’} Else {Write-Host ‘No Match’}

The first line creates a variable named $Data and sets it equal to ABCDEF. The second line checks to see if the variable’s contents match *. If the patterns match, the word Match is displayed; otherwise, the result is No Match. When we run these commands, the result is a match because an asterisk is a match for any value.

Image of a PowerShell if statement using an asterisk wild card.
The result is a match; thanks wildcard!

It’s also possible to use the asterisk in other ways. For example, if we were to rewrite the expression above and replace * with A*, then the expression would check to see if the value stored in $Data starts with the letter A. 

The asterisk tells PowerShell to ignore anything that comes after the A. If, on the other hand, we were to change A* to *A, then PowerShell would check to see if the value stored in $Data ends in the letter A. Again, it does not matter what comes before the letter A. You can see an example below.

Image of a PowerShell console showing three commands, the first defining $data, and two if statements returning match and no match results when using A and A wildcards respectively.
The first command was a match since ABCDEF starts with A, but the second command was not a match because ABCDEF does not end with the letter A.

Match a Single Character

Now, let’s use a different technique to evaluate an unknown character in the middle of a string. For this one, we’ll have to use a substring to identify the character’s position that we want to check.

For this example, let’s check if the third character in a string is equal to the letter C. As you may recall, $Data is equal to ABCDEF. PowerShell treats the first character as position 0, so the third character is in position 2. If we want to evaluate a single character instead of multiple characters, we must specify the number 1. Here’s what it looks like:

$Data.substring(2,1)

In other words, we are looking at one character in position 2. If we executed this line, then the result returned would be C. 

So if we wanted to write a line of code that would check to see if the third character was equal to C, the command would look like this:

If ($Data.substring(2,1) -Like ‘C’) {Write-Host ‘Match’} Else {Write-Host ‘No Match}

You can see how this works below:

Screenshot of PowerShell window showing the letter C produces a match, but the letter D does not for $data when using the two if statements.
The letter C produces a match, but the letter D does not.

Match Multiple Characters

Another common way to search a text string is to look for a string within a string. Suppose, for example, that we wanted to search $Data (ABCDEF) for the string CDE. To do so, we could use a variation of one of the techniques I showed you earlier. 

This time, we have an asterisk at the beginning and end of the evaluation criteria with the string we are looking for in the middle (*CDE*). Here is what the command looks like:

If ($Data -Like ‘*CDE*’) {Write-Host ‘Match’} Else {Write-Host ‘No Match’}

Screenshot of a PowerShell console displaying the string CDE returned a match while CDF did not.
The string CDE was a match, but CDF was not.

Next, we’ll go over working with PowerShell Wildcard brackets.

PowerShell Wildcard Brackets

One last trick I want to show you is using PowerShell to find a character in a particular range. Earlier I showed you how to check to see if the third position in a string was equal to the letter C. But what if this particular position contained an alphanumeric code, and we needed to check to see if the code fell into a specific range? 

The command below would look to see if the position in question contained the letters C, D, or E:

If ($Data.substring(2,1) -Like ‘*[C-E]*’) {Write-Host ‘Match’} Else {Write-Host ‘No Match’}

As you can see, our comparison is [C-E], meaning that we are checking for the letters C-E. You can see how this works below.

Screenshot of a PowerShell window showing how you can check the position of a character for a range of characters.
PowerShell can check a position for a range of characters.

Congratulations! You’re now an expert at wildcard options in PowerShell. Let’s wrap up with a few final thoughts.

Final Thoughts

Understanding how to properly query a string in PowerShell is essential to working with data. To this end, learning to use question marks as PowerShell Wildcards for characters you’re either unsure about or need a match on every returned string will keep you in good stead. 

Also, remember when you don’t know multiple characters like file extensions, suffixes, or prefixes, use asterisks to help. If you use two asterisks inside a string, any length of characters in between them will be returned.   

Learn more about PowerShell and related topics in our FAQ and Resources sections below!

FAQ

How do you add help functionality to a customized cmdlet?

You can use the Get-Help cmdlet to add help functionality to any customized cmdlet you create. This helps users understand the available options and syntax they should use when using your customized cmdlet. Others might use your cmdlet in the future; making them independent allows you to focus on your work.   

Does PowerShell have a way of looking for a wildcard value in a single position?

Yes, you can use the question mark to tell PowerShell that you want to query a string with one unknown character in a particular position. Results will return every string value that fits the criteria. For instance, if you’re looking for a file called “cat.jpg” but are unsure if it has an “a” or not, you can use a question mark as a placeholder. So, querying “c?t.jpg” could return cat.jpg, cit.jpg, and cct.jpg.  

Is there a way to look up which character is in a particular position and write that character to a variable?

You could combine a variable assignment with a substring to write a positional character to a variable. The command for doing so would look something like $Char=$Data.Substring(2,1). Values in parentheses here are position and character length, respectively. Note that $Data is the name of the variable assigned to the data; it could be anything in practice. 

Why are the evaluation expressions written to use -Like instead of -eq?

When you use -eq, you are telling PowerShell that you want to know if two values are equal. From a technical standpoint, a wildcard value is never truly equal to a literal text value. That’s why you have to use -Like instead of -eq.

Could you use techniques like these to search the contents of a text file?

PowerShell includes a cmdlet called Get-Content that you can use to read a text file. You can map a variable to this command to write the file’s contents to a variable. From there, you can use the various expressions as-is to search the file contents.

Resources

TechGenix: Article on PowerShell Global Variables

Read about PowerShell global variables.

TechGenix: Article on Taking Your PowerShell to The Next Level

Discover how to become more efficient with PowerShell.

TechGenix: Article on PowerShell’s $_. Variable

Discover how PowerShell’s $_. variable works.

TechGenix: Article on Mapping PowerShell Cmdlets to Variables

Learn about mapping PowerShell cmdlets to variables.

TechGenix: Article on Managing Exchange Online With PowerShell

Get to grips with PowerShell-based management for Exchange Online.

About The Author

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top