sudo apt install git make gawk
After that close and reopen your terminal and you should have autosuggestions enabled:


In this article, we will explore everything you need to know about PowerShell comparison operators, including their types, syntax, and examples. We’ll also look into their benefits and best practices for using them effectively.
Introduction to PowerShell Comparison Operators
Before we dive into the details, let’s define what PowerShell comparison operators are and why they are important. Comparison operators are symbols or words used to compare two values and return a Boolean (true or false) result. PowerShell uses comparison operators to evaluate expressions, test conditions, and perform logical operations. In PowerShell, comparison operators are used to compare strings, numbers, and other objects, and they are a key component of conditional statements, loops, and filtering operations.
PowerShell’s comparison operators are symbols or words used to compare two values and return a Boolean (true or false) result. There are a variety of comparison operators available in PowerShell, including equals, not equal, greater than, greater than or equal, less than, and less than or equal. These operators are used to compare strings, numbers, dates, boolean, and other objects, and they are a key component of conditional statements, loops, and filtering operations.
List of Comparison Operators in PowerShell
Here is a list of commonly used comparison operators in PowerShell:
-neNot equal to
-geGreater than or equal to
-leLess than or equal to
-notlikeNegative wildcard comparison
-matchRegular expression comparison =
-notmatchNegative regular expression comparison
-notcontainsNegative containment operator
-inChecks if a value is in a set of values
-notinChecks if a value is not in a set of values
-isReturns True if the object on its left-hand side is of the type specified on its right-hand side.
-isnotReturns True if the object on its left-hand side is not of the type specified on its right-hand side.
-replacereplaces strings matching a regex pattern
Please note that PowerShell comparison operators are case-insensitive by default. If you want to perform a case-sensitive comparison, you can use the case-sensitive versions of these operators, which are the same but with a ‘c’ prefix, like -ceq, -cne, -clike, -cnotlike, -cmatch, and -cnotmatch. Similarly, there are case-insensitive versions of the operators with an ‘i’ prefix, like -ieq, -ine, -ilike, -inotlike, -imatch, and -inotmatch.
Real-world examples of using PowerShell comparison operators
In real-world scenarios, PowerShell comparison operators are used extensively to perform various tasks. Some examples include:
These examples highlight the versatility and importance of PowerShell comparison operators in everyday scripting tasks.
Syntax of Comparison Operators
In this example, we are using the equality operator (-eq) to compare $value1 and $value2. If the values are equal, the expression will evaluate to $true; otherwise, it will evaluate to $false.
Types of Comparison Operators
PowerShell includes several types of comparison operators, each with its own syntax and purpose. Here is a brief overview of the different types of comparison operators:
Equality Operators
In this example, we are comparing $value1 and $value2 using the equality operator (-eq). Since the values are not equal, the expression will evaluate to $false, and the output will be “The values are not equal.”
Using the Greater Than Operator
This script uses the PowerShell greater than operator to compare the value of the $age variable to the integer 18. If the value of $age is greater than 18, the script outputs, “You are old enough to vote.”. If the value of $age is less than or equal to 18, the script outputs: “You are not old enough to vote.”.
Less than or equal, greater than or equal, not equal operators
To better understand how PowerShell comparison operators work in practice, here are some examples of how they can be used in scripts:
These examples demonstrate the practical usage of PowerShell comparison operators and showcase their versatility in different scenarios.
Using the Equals Operator for String Comparison
The -eq operator checks whether two strings are equal. For example,
This script uses the PowerShell equals operator to compare the value of the $name variable to the string “John”. If the two values are equal, the script outputs “Hello, John!”. If they are not equal, the script outputs, “Who are you?”.
Case-Sensitive Compare Operators
The PowerShell case-sensitive compare operators (-ceq, -cne, -cgt, -cge, -clt, and -cle) are used to compare strings in a case-sensitive manner. For example:
Pattern Matching Operators
In this example, we are using the matching operator (-like) to check if the $name variable starts with “John”. Since the name does start with “John”, the expression will evaluate to $true, and the output will be “The name starts with ‘John’.”
Here is another example to check if $Filename ends with .txt using the -like operator.
$Filename = “Document.txt”
$Filename -like “*.txt” # This will return True
Similarly, you use the -match operator in PowerShell to find a match for a pattern in a string using regular expressions. Here’s a simple example:
# Define a string
$string = “Hello123”
# Use -match to find a pattern
$result = $string -match “d” # d matches any digit
# Output result
$result # Returns True as the digit is found
In this script, the -match operator looks for the pattern “d” (which represents any digit in regular expressions) in the string “Hello123”. Since there are digits in “Hello123”, $result is True.
Containment Operators
In this example, we are using the containment operator (-contains) to check if the $numbers collection contains the number 3. Since the collection does contain the number 3, the expression will be evaluated $true, and the output will be “The collection contains the number 3.”
Let’s take a look at how the “In” operator can be used:
$Names = “John”, “Jane”, “Jack”
“Jane” -in $Names # This will return True
“Jill” -notin $Names # This will return True
Type Operators
$name = “John Doe”
$newName = $name -replace “Doe”, “Smith”
Write-Host “The new name is: $newName”
In this example, we are using the replacement operator (-replace) to replace the specified value last name “Doe” with “Smith”. The output will be “The new name is: John Smith”.
Date comparison Operators in PowerShell
PowerShell provides date comparison operators that allow you to compare dates. These operators include -eq (equals), -ne (not equals), -gt (greater than), -lt (less than), -ge (greater than or equal to), and -le (less than or equal to). These operators are useful when you are working with dates and need to perform comparisons.
The Compare-Object cmdlet in PowerShell
Apart from the basic comparison operators, PowerShell provides advanced techniques for comparison. One such technique is using the Compare-Object cmdlet, which allows you to compare two sets of objects and identify the differences between them.
The Compare-Object command is particularly useful when you need to compare complex objects or compare objects based on specific properties. It provides options to specify the properties to compare, the comparison mode (exact, ignore case, or ignore whitespace), and more.
Comparing Two Arrays using Compare-Object in PowerShell
Using PowerShell comparison operators offers a number of benefits, including:
Overall, using PowerShell comparison operators can help you write more efficient, flexible, and accurate scripts.
Tips for Using PowerShell Comparison Operators
To get the most out of PowerShell comparison operators, keep these tips in mind:
How to Master PowerShell Comparison Operators?
Mastering PowerShell comparison operators is crucial for effective scripting and automation. In this comprehensive guide, We have explored everything you need to know about PowerShell comparison operators, including their benefits, and advanced techniques for using them effectively.
What are comparison operators in PowerShell?
Comparison operators in PowerShell compare values and return a Boolean result (true or false) based on the comparison. You can use them in conditional statements, loops, and filters to make decisions by comparing values.
What is the difference between -eq and -like?
The -eq operator is used to compare exact matches, while the -like operator is used to compare partial matches using wildcards.#Using -eq operator”Hello, World” -eq “Hello, World” # Returns True#Using -like operator”Hello, World” -like “Hello*” # Returns True
Can I use PowerShell comparison operators with arrays?
Yes, you can use comparison operators with arrays to compare values. #Define arrays$array1 = 1,2,3,4,5$array2 = 1,2,3,4,5#Compare arrays$result = (Compare-Object -ReferenceObject $array1 -DifferenceObject $array2) -eq $null#Output result$result # Returns True as two arrays are equal
What is the not match operator in PowerShell?
What are the boolean comparison operators in PowerShell?
In PowerShell, the primary boolean comparison operators are -and, -or, and -not. Here’s an example:#Define boolean values$bool1 = $true$bool2 = $false#Using -and operator$bool1 -and $bool2 # Returns False#Using -or operator$bool1 -or $bool2 # Returns True#Using -not operator-not $bool1 # Returns False-not $bool2 # Returns True
How do I check if two values are equal in PowerShell?
How do I check if two strings are equal in PowerShell?
In PowerShell, you can compare two strings for equality using the -eq operator. Here’s a simple example:”Hello, World” -eq “Hello, World” # Returns True
How can I check if a string matches a specific pattern using wildcard characters in PowerShell?
How do I check if a collection in PowerShell contains a value?
How can I check the type of an object in PowerShell?
How do you use the -match operator?
The -match operator uses regular expressions to compare strings, checking if the string on the left matches the pattern on the right. For example, ‘PowerShell’ -match ‘^P.*ll$’ returns True because “PowerShell” starts with ‘P’ and ends with ‘ll’.

The -like operator in PowerShell provides a simple way to perform string matching and filtering. While you can use the -eq and -contains operators for basic string matching, the -like operator offers more flexible and powerful wildcard pattern matching capabilities. In this comprehensive guide, we will explore everything you need to know about like in PowerShell, including its syntax, operators, and advanced usage of -like to find text patterns in strings and filter collection objects.
We’ll also cover the wildcard in a switch statement, using -like and -notlike to filter collections, combining -like with other logical operators, and more through easy-to-understand examples. By the end of this guide, you will be able to master the PowerShell like and improve your PowerShell skills. Let’s get started mastering the -like operator in PowerShell!
Introduction to PowerShell -Like Operator
In this syntax, $string is the string that you want to compare, and “pattern” is the pattern that you want to match against the string. The pattern can include wildcard characters, such as * and ?, which allows you to match multiple characters or single characters, respectively.
This command will return True, as the string “Hello” starts with the letter “H”.
For a single-character match, you’d use ?.
Here are some common -like patterns:
This makes -like very flexible for pattern matching in strings.
Making -like Case-sensitive
The like operator in PowerShell performs case-insensitive comparison by default, meaning it will match strings regardless of their case. However, you can use the -clike operator to perform a case-sensitive comparison.
The -clike parameter is the case-sensitive version. This matches “Hello” to “h*”.
PowerShell Where Like – Filter and Search Strings
With PowerShell’s pipeline capability, You can combine the Where-Object to filter arrays or collections using -like. This command is commonly used in PowerShell scripts and commands to filter and search for specific strings in a collection of files, folders, or other objects.
In this syntax, $collection is the collection of objects that you want to filter, and “pattern” is the pattern you want to match against the specified property of each object in the collection. The -like operator is used to perform the string comparison.
Filtering Arrays with Where-Object
A common use case for -like is to filter a collection of strings, E.g.,
This would return “PowerShell”, “PowerPoint”, and “PowerApps” because they begin with “Power”.
Here is another example:
$Files = “file1.txt”,”file2.csv”,”photo.jpg”
TextFiles = $files -like “*.txt”
This command will return a collection of file objects with the word “report” in their name.
The “NotLike” Operator in PowerShell
Sometimes, you’ll want to retrieve elements that don’t match a pattern. The NotLike operator works similarly to the -NE operator, but the right-hand side operator may contain wildcards. Use the -notlike operator for this.
$strings -notlike “pattern”
Now “Alisha” is returned because it does not start with “Alic”. We can also filter other kinds of objects, like processes:
This gets all processes except those containing “Shell” in the name.
PowerShell If Like – Conditional Statement
PowerShell if like is a conditional statement that allows you to perform an action based on whether a string matches a specific pattern or not. This statement is commonly used in PowerShell scripts and commands to perform an action based on the contents of a string.
In this syntax, $string is the string that you want to compare and “pattern” is the pattern that you want to match against the string. The code inside the curly braces will be executed if the string matches the specified pattern.
This command will output the message “The string starts with the letter A”, as the string “Apple” starts with the letter “A”.
Combining Like Operator with Logical -And, -Or, -Not Operators
PowerShell -like operator can be combined with logical operators like -and , -or, and -Not for multiple comparisons. This combination is commonly used in PowerShell scripts and commands to perform complex string comparisons.
$string -like “pattern1” -or $string -like “pattern2”
In this syntax, $string is the string that you want to compare and “pattern1” and “pattern2” are the patterns that you want to match against the string. The -like operator is used to perform the string comparisons, and the -or operator is used to combine them using the OR condition. E.g.,
$string -like “Hello” -and $string -notlike “Bye”
This command will output the message “The string starts with A or ends with B”, as the string “Apple” starts with the letter “A”. Similarly, the AND logical operator can be used as:
This Returns “PowerA”. We can also negate the entire statement with -not:
-not ($string -like “Hi” -or $string -like “Hello”)
-like vs. -match, -like vs. contains
-like vs. -match:While -like uses wildcards for pattern matching, -match operator uses regex pattern (regular expressions). They are powerful but also more complex.
“PowerShell” -match “^Power” # True, using regex to match the start of a string
-like vs. contains:
The contains operator checks if an array contains a specific value, whereas -like checks if a string matches a pattern.
Matching Multiple Values with -Like in PowerShell
Here is an example of using the PowerShell -like operator to match multiple values in a string:
$string = “cat, dog, turtle”
$string -like “*cat*”
$string -like “*dog*”
$string -like “*turtle*”
This searches the string for the presence of “cat”, “dog”, and “turtle” using -like with wildcards.
To match the entire string, we can chain -like with -and:
$string -like “*cat*” -and $string -like “*dog*” -and $string -like “*turtle*”
Now, it will only return True if all three animals are found. Here is another example of using -Or operator:
This gets all the values with special characters !#$: alice!, bob#, charlie$
Using -like with the Switch Statement
We can also use -like with a switch statement to check multiple patterns. The switch statement also supports -like for pattern matching:
This prints “Name starts with Jo” since $name matches the first pattern. Here is another way of using wildcard expression in the Switch statement:
This switches over $string and prints a message if any of the -like wildcard patterns match.
Escaping Wildcard Characters with Like Operator
To match literal characters like *, ? Instead of wildcards, escape them with regular expressions:
PowerShell String -Like Examples
Let’s look at some examples to help you understand how the PowerShell -like works in practice.
Matching a specific string
You can use it for comparing entire strings:
This command will output the message “The string matches”.
Matching a pattern using wildcards
This command will output CAT45123, CAT45234.
You want to get all lines from a log file where an error or warning is logged:
Filter Files using NotLike Operator
Let’s filter files by specific type using NotLike:
This filters out all the JPG and PNG files.
Single Character Pattern Matching
This command will output the message “The string matches the Employee number pattern”, as the string matches the pattern for an employee number. Here, each ? matches any single character. This will match any string that fits the format of three characters, a hyphen, three more characters, another hyphen, and then four characters.
Recap and Key Takeaways
At its core, the -like operator in PowerShell facilitates wildcard string comparison. It checks if a string conforms to a specified pattern, enabling both exact and fuzzy matches. The main points about PowerShell’s -like operator covered in this post include:
In summary, the -like operator provides powerful string pattern matching capabilities in PowerShell, similar to SQL queries. Using -like and -notlike makes it easy to search and filter text strings in your PowerShell scripts. PowerShell also supports a wide range of comparison operators (Eq, Ne, Lt, Gt, Ge, Le, etc.). Here is my other article: How to use Comparison Operators in PowerShell?
Conclusion and Next Steps
What is the -like operator in PowerShell?
The -like operator in PowerShell is used to perform wildcard pattern matching on strings. It allows you to check if a string matches a specified wildcard pattern. For example: $filename -like ‘*.txt’
How do I use the -Like operator to match a specific pattern?
To use the -Like operator for pattern matching, you can specify the pattern using wildcard characters. For example: “apple” -Like “a*” will return $true because the string “apple” starts with the letter “a”.
What are the wildcard characters used with -like?
Can I use multiple wildcard patterns with -like?
Yes, you can use multiple wildcard patterns by separating them with the -or operator. For example: $FileName = “C:Tempile.txt”$FileName -like ‘.txt’ -or $FileName -like ‘.docx’
How does -like differ from -match?
Can I negate the -like operator?
Yes, you can negate the -notlike operator. This checks if the string does not match the specified wildcard pattern: $FileName -notlike ‘*.txt’
Is -like case-sensitive?
By default, -like is not case-sensitive. However, you can make it case-sensitive by using the -clike (case-like) operator.
Can I use the -Like operator with arrays or collections?
Yes, you can use the -Like operator with arrays or collections. It will return all the elements that match the specified pattern. For example: “apple”, “banana”, “cherry” -Like “*a*” will return “apple” and “banana”.
How can I use the -like operator to filter an array or collection?
When folks are exploring the operators available in PowerShell, they’ll often run across two seemingly similar, but drastically different, operators — and get confused by them.
I’ll often see someone try this:
They’re trying to see if $string contains the letters “win,” but, unfortunately, that’s not what -contains does. What they really want to use is the -like operator:
I know it doesn’t read as nicely when you say it out loud. But -like is the correct operator for this task.
The -contains operator is a bit trickier. It’s designed to tell you if a collection of objects includes (‘contains’) a particular object. Now, if your collection of object is a bunch of strings, this is pretty straightforward.
It gets harder when the collection contains complex objects. This won’t work like you might think:
The variable $coll does not the string “BITS.” What it contains is a service object whose Name property is BITS. You can’t really use -contains in this fashion, because it compares the entire object.
This will almost always be False. That’s because in the time it takes to run the second command, the first process Its memory, CPU, or some other value has changed. Although $procs does contain the same actual process as $proc, not every single property is exactly the same in both instances. So -contains can’t detect the match.
PowerShell’s operators are working as designed, this is a simple logic bug in your code.
In your updated example, you’re getting differing breakdowns because of the way you’re applying the -like/-notlike operators to a collection of strings rather than a single string value.
When used this way, PowerShell’s comparison operators turn into sieves – they start filtering the input data:
When placed in the context of an if condition, PowerShell has to convert the expression output to a boolean – and PowerShell happens to treat non-empty arrays as $true for conversion purposes.
Regular expression, commonly referred to as regex, is a powerful tool if you know how to use it. In terms of PowerShell regular expressions, you can find and alter text strings, identify text strings that match, and much more. For example, regex can help you read and analyze a log file — a process that’s horrifying to think about doing manually.
Let’s dive into how regex can help you be l̶a̶z̶i̶e̶r̶ more efficient in your role.
What is regular expression (regex)?
Regular expression is a series of characters that define a search pattern. You can use regex to identify matching text strings. It’s like a really fancy find-and-replace feature that helps you sift through large amounts of data (or text strings) at once. Regular expressions can consist of a single character or a complex combination of characters designed to return broad or very concise results. In other words, it’s built for you to fine tune to meet your needs.
Regular expression works with many text editors and scripting languages, including Notepad++, Visual Studio Code, SQL, Python, Perl, Java, PowerShell, and many more. While some systems include their own flavor of regex, others use standard regex libraries. PowerShell uses .NET regular expressions.
The information in this article focuses solely on the .NET regex engine. While there are many similarities between regex implementations, there can also be several differences between syntax, features, and behavior.
PowerShell regular expression reference sheet
If you’re new to regex and come across a complex regex statement, then you know what true confusion feels like.
Regex is a somewhat standardized shorthand language model that uses special characters, sometimes called metacharacters, to define pattern parameters. I say “somewhat” because different applications and languages use different regex models, which can vary slightly.
To the untrained eye, a regex statement may appear as though a cat walked across a keyboard. However, even those familiar with regex may struggle to read patterns, especially if they don’t work with regex consistently. That’s why it’s nice to keep a cheat sheet handy.
Matches any single character except newline.
Matches the beginning of a line.
Matches the end of a line.
Escapes a trailing special character.
Matches zero or more times, matching as many times as possible.
Matches 1 or more times, matching as many times as possible.
Repeat 0 or 1 time, as many times as possible.
Repeat 0 or 1 time, 0 if possible.
Repeat 0 or more times, as few as possible.
Repeat 1 or more times, as few as possible.
Matches a non-alphanumeric character.
Matches any non-number character.
Matches a whitespace character.
Matches any non-whitespace character.
Matches a newline.
Matches a tab.
Matches a carriage return.
Matches a word boundary between word and nonword characters.
Matches a location that is not a word boundary.
Matches must occur at the beginning of a string.
Matches must occur at the end of a string or before a newline.
Matches must occur at the end of a string.
Matches must occur at the point the previous match ended.
Matches any character in brackets.
Matches any character not in brackets.
Matches any character in the range of characters.
Matches exactly n times.
Matches at least n times.
Matches at least n times, up to m times.
Matches either a or b.
Specifies the pattern as a group.
Using regex in PowerShell
If you’ve spent any significant amount of time with PowerShell, there’s a good chance you’ve already used regex. If you’ve matched or replaced a string, you’ve definitely used regex.
Let’s look at a few basic examples of regex usage in PowerShell. Remember, if you’re wondering what a regex special character does, refer to the table above or any of the other resources linked below.
Matching patterns in PowerShell with regex
The -match operator identifies input that matches the provided regex pattern. It returns a Boolean value of true if a match is found and false if no match is found. Here is an example of how the -match operator works.
‘Elden Ring is an amazing game!’ -match ‘amazing’
This command returns true because the pattern ‘amazing’ is found in the string ‘Elden Ring is an amazing game!’

Be careful not to confuse -match with -like. While -match uses regex, -like uses simplified wildcard pattern matching. If we replace -match with -like in the previous example, the command returns false.

The -like operator attempts to match the pattern ‘amazing’ exactly. It fails because the sentence contains much more than just ‘amazing.’ If we wanted the above command to work with the -like operator, we would need to add an asterisk before and after ‘amazing’ for the command to return true.
Here’s a slightly more complex example of utilizing the -match operator.

Replacing strings in PowerShell with regex
The -replace operator in PowerShell works much like the -match operator, except when a matching pattern is found, it calls for that pattern to be replaced with a substitute. Here’s the basic format of a -replace command:
Here’s a simple example of a -replace command that uses variables to store the input.
$halo = ‘Halo infinite was great.’
$halo -replace ‘was great’, ‘could have been better’

It’s important to note that the -replace operator doesn’t overwrite the value stored in the variable. You would need to recast the value of the variable to replace it.
Splitting strings in PowerShell with regex
The -split operator in PowerShell can split strings into substrings. By default, whitespace is used as the delimiter. However, the delimiter can be set to specific characters, strings, or patterns.
Here are some examples of the -split operator in use.
-split ‘Red Dead Redemption’
‘Red,Dead,Redemption’ -split ‘,’
‘RedzDeadzRedemption’ -split ‘z’
‘Red1Dead2Redemption’ -split ‘d’

The first example is formatted a bit differently but uses the default whitespace as the delimiter. The second and third examples use ‘,’ and ‘z’ characters as the designated delimiters. The last example uses the ‘d’ regex special character, meaning any number character is treated as a delimiter.
Using Select-String in PowerShell
The Select-String cmdlet uses regex to match input from strings and files. If you have data that needs to be cleaned up and formatted, the Select-String cmdlet is the right tool for the job.

In this example, we piped the contents of the $text variable to the Select-String cmdlet. Using the -Pattern parameter, we set the regex pattern as ‘MasssEffectsd’ and set the pattern to be case sensitive with the -CaseSensitive parameter. We also included the -AllMatches parameter because only the first match in each line returns by default. The -AllMatches parameter returns all matches found. We’ve assigned the results of the command to the $games variable, then called the variable with the .Matches.Value property to return just the matched values. Notice that ‘mass effect 4’ was not returned because it did not match the case sensitivity requirement.
Here is what our original source file looks like.

Here is the resulting CSV file.

Using switch statements with the -Regex parameter
The switch statement in PowerShell uses exact matching by default, but we can employ the -Regex option to force it to use regex instead. Switch statements are great for evaluating multiple conditions.
Here’s what the syntax of a simple switch statement that uses the -Regex parameter looks like.


While this example is certainly effective, it doesn’t rely on complex regex patterns because it doesn’t have to. Your regex patterns need to meet only your requirements. In this example, the requirements were simple, and the patterns could match the information contained in the CSV file almost exactly.

Other regex resources
Regex is not for the faint of heart, but there are tons of great resources available when you need help. Here are some terrific resources that can help both regex beginners and pros.
Regex is powerful but complex
There is so much you can do with regex. You can parse files, scrape websites, validate input, and so much more. However, it’s complex. If you don’t regularly use it, you’ll soon forget much of what you learned. That’s why there’s no shame in frequently using the resources above.
If you’re looking for powerful tools that aren’t complex, check out PDQ Deploy & Inventory. This powerful combination can help you manage your endpoints and automate deployments with minimal effort. Try PDQ Deploy & Inventory for free for 14 days.

Born in the ’80s and raised by his NES, Brock quickly fell in love with everything tech. With over 15 years of IT experience, Brock now enjoys the life of luxury as a renowned tech blogger and receiver of many Dundie Awards. In his free time, Brock enjoys adventuring with his wife, kids, and dogs, while dreaming of retirement.



