PowerShell provides various comparison operators, which allow users to perform various string and pattern-matching operations. Among these operators, the -like operator in PowerShell is useful for pattern matching using wildcard characters.
In this blog tutorial, I will explain everything about the PowerShell -like operator, including its syntax, usage, and practical examples.
What is the -like Operator in PowerShell?
The -like operator in PowerShell is used to compare strings using wildcard characters. It returns True if the string on the left side matches the pattern specified on the right side. This operator is case-insensitive by default.
Syntax
The syntax of the PowerShell -like operator is:
<string> -like <pattern><string>: The string you want to compare.<pattern>: The pattern you want to match, which can include wildcard characters like*and?.
Wildcard Characters
*: Matches zero or more characters.?: Matches exactly one character.
Read -and Operator in PowerShell
Examples of Using the -like Operator in PowerShell
Here, I will show you some examples of how to use the -like operator in PowerShell.
Example 1: Basic String Matching
$input = "California, USA"
if ($input -like "California*") {
Write-Output "The input starts with 'California'."
}In this example, the -like operator checks if the string $input starts with “California”. Since the pattern “California*” matches any string that begins with “California”, the condition is True, and the script outputs “The input starts with ‘California’.”
I executed the above PowerShell script, and you can see the output in the screenshot below:

Example 2: Using the ? Wildcard
Here is an example where I have taken an example to show you how to use ? with -like operator.
$filename = "report1.txt"
if ($filename -like "report?.txt") {
Write-Output "The filename matches the pattern."
}Here, the pattern “report?.txt” uses the ? wildcard to match any single character in place of the ?. Since “report1.txt” fits this pattern, the condition is True, and the script outputs, “The filename matches the pattern.”
Check out Split a String by Word in PowerShell
Example 3: Match Substrings using -Like
This is another example of how to use the -Like operator to match substrings in a string in PowerShell.
$input = "PowerShell scripting"
if ($input -like "*script*") {
Write-Output "The input contains the word 'script'."
}In this example, the pattern “script” matches any string that contains the substring “script”. Since “PowerShell scripting” includes “script”, the condition is True, and the script outputs “The input contains the word ‘script’.”
You can see the output after I executed the script using VS code.

Example 4: Case-Insensitive Matching
Now, here is another example where I have explained how to do case-insensitive matching using the -Like operator in PowerShell.
$input = "PowerShell"
if ($input -like "powershell") {
Write-Output "The input matches 'powershell' (case-insensitive)."
}PowerShell’s -like operator is case-insensitive by default. Therefore, “PowerShell” matches “powershell”, and the script outputs “The input matches ‘powershell’ (case-insensitive).”
Example 5: Filter Data using -Like operator
Now, let me show you another example of how to use the -Like operator to filter data in PowerShell. For example, you can use it to filter files in a directory:
Get-ChildItem -Path "C:\Logs" | Where-Object { $_.Name -like "*.log" }This command lists all files in the “C:\Logs” directory with a “.log” extension.
Check out Create Log File In PowerShell
Example 6: Validate User Input
You can also use the -like operator to validate user input in PowerShell. For example, checking if an email address format is correct. Here is the complete script.
$email = "user@example.com"
if ($email -like "*@*.*") {
Write-Output "The email address format is valid."
} else {
Write-Output "The email address format is invalid."
}Conclusion
The -like operator in PowerShell is used for pattern matching and string comparison.
In this tutorial, I have explained six different examples of using the -Like operator in PowerShell.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.