
One of the most common tasks in PowerShell is working with strings, and a string comparison is an essential part of this task. PowerShell has multiple ways to compare strings, and each approach is appropriate for different scenarios. Whether it’s to determine if two values are the same, or to search for specific patterns within strings, efficient string comparison is a crucial skill to master.
Table of contents
- PowerShell String Comparison Operators
- PowerShell to Compare Strings using -eq Operator
- Comparing Strings with Wildcards using the -like Operator
- PowerShell string comparison using -match operator
- Comparing Strings in Collections using -Contains operator
- Handling Null or Empty Strings
- Tips and tricks for efficient string comparison in PowerShell
- Common mistakes to avoid when comparing strings in PowerShell
- Wrapping up
PowerShell String Comparison Operators
| Operator | Description |
|---|---|
| -eq | Equal to – checks if two values are equal. (Please note: = is the assignment operator in PowerShell!) |
| -ne | not equal to – determines if two values are not equal. |
| -like | like operator – allows you to compare strings using wildcard patterns |
| -notlike | not like operator – checks if a string does not match a specified pattern |
| -match | match operator – compares a string to a regular expression pattern |
| -notmatch | not match operator – checks if a string does not match the specified pattern |
| -contains | Contains operator determines if a collection contains a specific value |
| -notcontains | notcontains operator checks if a value is not present in a collection |
PowerShell to Compare Strings using -eq Operator
The -eq operator is useful if you want to check if two strings are exactly equal (It is case-insensitive by default, meaning it disregards the case of the characters in the strings). If you don’t need an exact comparison, there are other operators that may be more suitable for your needs.
This will return False since the two strings are not equal. However, if we compare “Hello” to “Hello”, the command will return True.
Example: PowerShell if equals string comparison
$UserInput = Read-Host "Enter a string"
if ($userInput -eq "Hello") { Write-Output "The strings are equal"
}
else { Write-Output "The strings are not equal"
}Case-Insensitive Comparison
$String1 = "Hello" $String2 = "hello" $String1 -ceq $String2
The output would be “False” since the two strings are not equal in the casing (In other words, the Uppercase string is not equal to the lowercase). We have similar operators for like (clike), match (cmatch), and contains (ccontains)
Compare Two Strings using Equals() method
We also have the “.Equals()” method in the string to compare the Contents of Two String Objects in PowerShell.
Returns False as the Equals() method compares the string in case-sensitive matching.

The Not Equal Operator (-ne)
If the two strings are not equal, this command will return true; otherwise, it will return false.
Comparing Strings with Wildcards using the -like Operator
"hello world" -like "hello*"
This will also return True since “Hello” ends with the letter “o”. Similarly, you can compare the single character as:
"PowerShell" -like "Power???ll"
PowerShell string comparison using -match operator
There are also other ways to compare strings in PowerShell, such as using the -match and -notmatch operators, which use regular expressions for matching. The -match operator returns true if a string matches a regular expression pattern, while the -notmatch operator returns true if a string does not match a regular expression pattern. Regular expressions are patterns used to match character combinations in strings.
"Hello, I have a cat" -match "cat"
The above example will return True since the string contains the word “cat”. However, if we search for all strings that contain the word “dog”, the command will return False.
Comparing Strings with Regular Expressions
Regular expressions are powerful tools for searching and manipulating text-based data. They allow you to quickly find what you’re looking for by searching through large amounts of data. In PowerShell, RegEx can be used with the -match and -notmatch operators to perform complex searches on strings. They are more complicated than other comparison operators but provide a great deal of flexibility when searching through text-based data.
$Text = "PowerShell is a powerful scripting and automation tool" $Text -match "PowerShell\s\w+"
"The price is 12 dollars." -Match "\d+"

Comparing Strings for Substrings
You can use the -match operator to determine if one string is a substring of another. This operator returns True if the second string is found within the first string. Here’s an example:
$string1 = "hello world"
$string2 = "world"
If ($string1 -match $string2) { Write-Host "The second string is a substring of the first string."
} Else { Write-Host "The second string is not a substring of the first string."
}In this example, the output would be “The second string is a substring of the first string” because the string “world” is found within the string “hello world”.
Comparing Strings in Collections using -Contains operator
In PowerShell, you can compare strings within collections using the comparison operators. For example, if you have an array of strings, you can use the -contains operator to check if any of the strings in the array are equal to a specific value. Here’s an example:
$array = @("Hello", "World", "PowerShell")
$array -contains "World"In this example, the -contains operator compares each string in the array to the value and returns the matching strings. More on using the contains operator in PowerShell is here: How to use the contains operator in PowerShell?
Handling Null or Empty Strings
When comparing strings in PowerShell, it’s important to handle null or empty strings appropriately. You can use the -eq operator to check if a string is equal to null or empty. Here’s an example:
$string = "" [string]::IsNullOrEmpty($string) -eq $true
Tips and tricks for efficient string comparison in PowerShell
Here are some tips and tricks to help you master string comparison in PowerShell:
- Use the -eq operator for simple string comparison tasks.
- Use the -like operator for wildcard string comparison tasks.
- Use the -match operator for more complex string comparison tasks and regular expressions.
- Use the If statement in combination with string comparison for conditional actions.
- Use regular expressions to search for specific patterns within strings.
- Be aware of the case sensitivity of your string comparison. If you need a case-sensitive comparison, use the appropriate case-sensitive operators (E.g., -ceq). Otherwise, stick with the default case-insensitive operators.
- Handle null or empty strings
Common mistakes to avoid when comparing strings in PowerShell
Avoid these common pitfalls when comparing strings in PowerShell:
- Forgetting to use quotes around string values.
- Using the wrong comparison operator.
- Forgetting to convert objects to strings before comparing them.
- Not accounting for case sensitivity in string comparison.
- Be mindful of case sensitivity. Use -ceq or -clike for case-sensitive matching.
- Check for typos or spelling errors in your strings. Even a tiny difference in characters can cause a string comparison to fail.
Wrapping up
Whether you need to compare two strings for equality, determine if one string is a substring of another, or compare strings using wildcards, PowerShell has you covered. Each of these methods has its set of advantages and use cases, and you can use the one that best suits your needs.
Checkout my other related post on Comparison Operators in PowerShell: An Essential Guide
How can I perform a basic string comparison in PowerShell?
To perform a basic string comparison in PowerShell, you can use the -eq (equal) and -ne (not equal) operators. For example:"string1" -eq "string2"
This will return True if both strings are identical and False otherwise.
How do I compare strings in a case-insensitive manner?
PowerShell comparisons are case-insensitive by default. However, if you want to explicitly ensure case-insensitivity, you can use the -eq operator with the ToLower() or ToUpper() methods on both strings.
How can I perform a case-sensitive string comparison?
To perform a case-sensitive comparison, you can use the -ceq (case-sensitive equal) operator:"STRING" -ceq "string"
This returns “False” because it considers the case of each character in the comparison.
Can I compare strings for greater than or less than?
Yes, you can use the -gt (greater than), -lt (less than), -ge (greater than or equal to), and -le (less than or equal to) operators to compare strings in a lexicographical manner:"string2" -gt "string1"
This returns True if “string2” is lexicographically greater than “string1”.
How can I compare multiple strings at once?
How can I check if a string starts with or ends with a specific substring in PowerShell?
How can I combine string comparisons with conditional statements?
Is PowerShell case-sensitive?
PowerShell is generally case-insensitive, meaning that command names, parameter names, operator names, variable names, and environment variable names are not affected by the case used when typing them15. For example, the command get-process is equivalent to Get-Process or gET-PROCess. If you want to perform a case-sensitive string comparison, you can use the -ceq operator instead of -eq.
If statements are conditional logic statements that you can add to your PowerShell scripts. If statements mimic the decision-making process people use every day. If a condition is met, then something happens. For example, if it’s raining outside, I’ll grab an umbrella before heading outside.
If statements in PowerShell function similarly to If statements in other programming languages.

In this diagram, if the condition is true, then it runs a specific command or statement. If the condition is false, it moves onto the next command or statement. Here’s a simple PowerShell example.

In this example, we created a variable called $eggs and gave it a value of 10. Next, we set a conditional statement that says if $eggs is less than 12, display a message. Since $eggs has a value of 10, the message “You have less than a dozen eggs” is displayed.
Now that we have a basic understanding of If statements, let’s dive a little deeper and go over the syntax and some more advanced examples.
PowerShell If statement syntax
The syntax of If statements in PowerShell is pretty basic and resembles other coding languages.
The condition statement itself can be structured in various different ways. Many condition statements use comparison operators. In my earlier eggcelent example, I used the -lt comparison operation in my conditional statement, which stands for less than. Here is a list of some of the comparison operators you can use in PowerShell.
Keep in mind that condition statements don’t require comparison operators. You can use regular PowerShell cmdlets in the condition statement. For example:

In this example, we are calling the Test-Path cmdlet to see if a file exists or not. If the file exists, we use the Get-Content and Measure-Object cmdlets to return a word count of the file. If the file does not exist, then the script will just end. As you can see from the screenshot, my MacGyver biography is only 8 words long so far. One of these days, I’ll finish it.
PowerShell If-Else statements
Up to this point, we’ve only talked about If statements. However, you’ll often find If statements accompanied by an Else statement. Else statements allow you to perform an additional action if the condition is not met or returns false.

In this diagram, you can see that we now have two statements that can be executed. One statement if the condition returns true, and one statement if the condition returns false. Here’s a simple PowerShell If-Else statement example.

In this example, we’ve set the variable $x to a value of 4. We then set our If statement with the condition that if $x is greater than or equal to 3, display the message “$x is greater than or equal to 3.” Lastly, we set our Else statement that if the condition is false, display the message “$x is less than 3.”
You can see from the screenshot that since $x equaled 4, the condition returned true. Now let’s change the value of $x to 1, making the condition return false.

Now that the condition returns false, you can see that PowerShell is returning our Else statement, “$x is less than 3.”
Nesting conditional statements
The second and preferred way to nest conditional statements is to use the elseif statement. Here’s an example that builds on the egg example we covered earlier.

In this example, we have three possible outcomes. One if we have exactly 12 eggs. One if we have less than 12 eggs. And one if we have more than 12 eggs. In the screenshot above, we have our $egg variable set to 14, which returned the Else statement, displaying the message “You have more than a dozen eggs.”
Negating PowerShell statements
Sometimes with operators, we need to negate the statement. Using the previous MacGyver example, what if instead of searching for the MacGyver biography, we want to make sure it isn’t there? Here’s an example of how to do that:

Now that we know all about If statements, Else statements, and nested conditional statements, let’s bring it all together by creating a script that will give us our dinner plans depending on what day of the week it is.
First, we’ll get the day of the week using the Get-Date cmdlet, returning the DayOfWeek property and assigning it to the $day variable.
$day = (Get-Date).DayOfWeek
Next, we’ll build our nested conditional statement for the different days of the week and assign a different meal for each day.

Wrapping up
If you’re interested in other powerful tools, make sure you download a free trial of PDQ Deploy and PDQ Inventory. PDQ Deploy will help you keep your network environment up to date, making deployments and updates a breeze. PDQ Inventory will ensure you have all the information you need to properly manage all of your Windows devices. PDQ Inventory also gives you access to the PowerShell scanner, letting you take advantage of all the cool PowerShell knowledge you just learned in this article.

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.
Filter the PowerShell output result based on specific conditions. Use comparison operators to compare or find matching values when creating a script. In this article, you will learn how to use comparison operators in PowerShell.
PowerShell Comparison operator types
There are five types of comparison operators in PowerShell:
| Comparison Operator type | Comparison Operator examples |
|---|---|
| Equality | -eq, -ne, -gt, -ge, -lt, -le |
| Matching | -like, -notlike, -match, -notmatch |
| Replacement | -replace |
| Containment | -contains, -notcontains, -in, -notin |
| Type | -is, -isnot |
When you write a script, it’s best to use comparison operators to make it more efficient and get accurate results. Comparison operators let you compare values or find values that match specified patterns. It’s used to compare strings, numbers, dates, and other objects.
| Comparison Operator | Description |
|---|---|
| -eq | equal to |
| -ne | not equal to |
| -gt | greater than |
| -ge | greater than or equal to |
| -lt | less than |
| -le | less than or equal to |
| -like | like (use wildcard for pattern matching) |
| -notlike | notlike (use wildcard for pattern matching) |
| -match | string matches pattern |
| -notmatch | string doesn’t match pattern |
| -replace | replaces strings matching a regex pattern |
| -contains | collection contains a value |
| -notcontains | collection doesn’t contain a value |
| -in | value is in a collection |
| -notin | value isn’t in a collection |
| -is | both objects are the same type |
| -isnot | the objects aren’t the same type |
Connect to Exchange Online PowerShell
Connect-ExchangeOnlinePowerShell Comparison operators structure
An additional component we will use with the Comparison operators is Where-Object.
The Where-Object cmdlet, combined with the Comparison operators, enables us to create a PowerShell sentence that describes the required condition. In the next examples, we will use the shortened version of Where-Object, using the Where cmdlet.
We will show how to perform a PowerShell sentence structure with a specific condition.
PowerShell sentence structure:
- In the first part of the PowerShell sentence, we will use the Get-Mailbox cmdlet with -ResultSize Unlimited to find all the existing mailbox types.
- We use the pipeline (|) to get the input from the first part and do something with it in the second part.
- In the second part of the PowerShell sentence, we will use the Where cmdlet.
- The condition must be enclosed in braces brackets { }
- The $_. notation is used to represent the default object
- Specify the Property name
- Use the Comparison operator -eq
- Specify the Value in double quotation marks (“)
See the below PowerShell syntax example.
Get-Mailbox -ResultSize Unlimited | Where {$_.Property -eq "Value"}- Then, you can add an additional section using a pipeline (|) and Select cmdlet to specify the properties that will be displayed.
The PowerShell command syntax example with select properties.
Get-Mailbox -ResultSize Unlimited | Where {$_.Property -eq "Value"} | Select Property1, Property2Equality Comparison operator examples
There are unlimited possibilities when using Comparison operators. You can write a simple PowerShell sentence that includes one condition or write a complicated PowerShell sentence that is built from a combination of conditions.
We will show you different examples of using Equality Comparison operators. The PowerShell Comparison operators are case-insensitive by default. With the -c prefix, you can use the PowerShell case-sensitive Equality Comparison operators, such as -ceq, -cne, -cgt, -cge, -clt, and -cle.
Comparison operator -eq
Exchange online enables us to create different types of mailboxes, such as:
We will show you two different examples using the comparison operator -eq.

- In the first part of the PowerShell sentence, we will use the Get-Mailbox cmdlet, followed by -ResultSize Unlimited.
- In the second part of the PowerShell sentence, we will use the Where cmdlet, followed by a condition in braces brackets { }.
- Lastly, we use the property RecipientTypeDetails and the comparison operator -eq to filter the mailboxes with a value that equals “UserMailbox”.
Run the below PowerShell command example.
Get-Mailbox -ResultSize Unlimited | Where {$_.RecipientTypeDetails -eq "UserMailbox"} | Select Name, UserPrincipalName, RecipientTypeName UserPrincipalName RecipientType
---- ----------------- -------------
41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 Amanda.Hansen@m365info.com UserMailbox
82cd0d62-e974-4892-aca6-e0387abc62be Anna.Bell@m365info.com UserMailbox
5cae3874-442b-459c-8f33-3aee5b879275 Anne.Butler@m365info.com UserMailbox
0f38d53f-cbe0-4844-86e9-1032a45ba31b Brenda.Smith@m365info.com UserMailbox
411a8f10-0dfa-4034-a1e3-a8b6e4cad2f6 Brian.Mill@m365info.com UserMailbox
52a6c1c7-77d2-4109-99b9-a4076167b6e2 Carl.Hawk@m365info.com UserMailbox
d5dc703a-45d0-4dc3-89ba-16f088925e41 Charles.MitchelNEW@m365info.com UserMailbox
b602b148-2fcf-435a-9d34-ce72c3a8c748 Diana.Baker@m365info.com UserMailbox
29a12fd8-bbd2-440f-b457-8e304200a85d Frank.Olsen@m365info.com UserMailbox
2ab57f6a-6824-40a3-8fb0-d4357e8b91b1 John.Doe2@m365info.com UserMailbox
33a56fba-ed21-456d-90be-ade268b9acc8 Justin.Russel@m365info.com UserMailbox
12eefbb2-e5f4-4eec-bd18-df7ca2f1ee6b Ken.Walker@m365info.com UserMailbox
213a5a8b-0bcf-40cc-b114-edb1b2a423f4 Laura.Terry@m365info.com UserMailbox
3bb176aa-d0ba-47f7-aecc-f4837593006e Mary.James@m365info.com UserMailbox
8b31d6a0-6d92-4b5b-80bd-a4029e641d35 RoomMailboxTest@m365info.com UserMailbox
e5c3a5e8-eeae-4829-94dc-fb7228bcf8da Ryan.Walker@m365info.com UserMailbox
1e367b85-f0c0-4c9c-a16a-22d132f1d8e6 Soren.Vest@m365info.com UserMailbox
c32b2b27-d809-439a-a3e3-eb7a749eeb72 Stephen.Hunter@m365info.com UserMailbox
fd199cb4-2ebf-4171-96e2-12fd75453e39 Susan.Brown5@m365info.com UserMailbox
- In the first part of the PowerShell sentence, we will use the Get-User cmdlet followed by -ResultSize Unlimited cmdlet.
- In the second part of the PowerShell sentence, we will use the Where cmdlet, followed by a condition in braces brackets { }.
- Lastly, we use the property Department and the comparison operator -eq to display an exact match of users with a value that equals “Sales”.
Run the below PowerShell command syntax.
Get-User -ResultSize Unlimited | Where {$_.Department -eq "Sales"} | Select Name, RecipientType, DepartmentName RecipientType Department
---- ------------- ----------
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b User Sales
b602b148-2fcf-435a-9d34-ce72c3a8c748 UserMailbox Sales
f8261d51-3df9-4f21-a6b1-533412669c11 User Sales
bc741ead-e704-4eb9-a9e8-adfa1f187c92 User Sales
0de964ae-f09a-4e65-bc8c-cbeac2745e4c User Sales
27c03114-f9a1-4c02-83ef-bd4ee32f00cf User Sales
82cd0d62-e974-4892-aca6-e0387abc62be UserMailbox Sales
52a6c1c7-77d2-4109-99b9-a4076167b6e2 UserMailbox SalesComparison operator -ne

A more efficient approach is to use the comparison operator -ne. It means you want to find all the objects that don’t have the value “Ontario”.
Run the PowerShell command syntax example.
Get-User -ResultSize Unlimited | Where {$_.StateOrProvince -ne "Ontario"} | Select Name, DisplayName,RecipientType, StateOrProvinceName DisplayName RecipientType StateOrProvince
---- ----------- ------------- ---------------
4631b453-3a0a-439e-a2ea-7af2b9a741b3 Chris Green User Florida
f8261d51-3df9-4f21-a6b1-533412669c11 Cynthia Carey User Washington
652159b8-fc74-46bf-9d6f-d8a97d75e1eb John.Doe1 User Texas
bc741ead-e704-4eb9-a9e8-adfa1f187c92 John Doe2 UserMailbox Florida
ee042397-eb86-4c54-836a-7879df27ec1c Piers Wood User Texas
0de964ae-f09a-4e65-bc8c-cbeac2745e4c Susan Brown UserMailbox New York
e6f5f419-de0b-43c3-a1e8-52af2436f931 Victor Gray User TexasComparison operator -gt
When using the Equality Comparison operator -gt, you will only get an output if the value on the left is greater than the number on the right.
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.Department.Length -gt 5} | Select Name, RecipientType, DepartmentName RecipientType Department
---- ------------- ----------
41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 UserMailbox Marketing
67962421-00e7-448b-b382-83b7b434e41c User Sales UK
6ec79c8e-5a84-4992-b2df-4caf3f997a74 User Spain Sales
5cae3874-442b-459c-8f33-3aee5b879275 UserMailbox Marketing
2beda4b9-559b-4d39-9415-51ce47f2963f User Sales UK
33a56fba-ed21-456d-90be-ade268b9acc8 UserMailbox Sales USA
d23f1e98-02db-4292-98aa-6f511c433f1b User SalesWorldWide
ee042397-eb86-4c54-836a-7879df27ec1c User Brandsales
ca21f458-f33c-46bf-9f57-55c201ccbd82 User Sales USA
4631b453-3a0a-439e-a2ea-7af2b9a741b3 User Information Technology
7545348c-b473-45b5-890d-38bc59b4a163 User Information Technology
0f38d53f-cbe0-4844-86e9-1032a45ba31b UserMailbox MarketingComparison operator -ge
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.Department.Length -ge 2} | Select Name, RecipientType, DepartmentName RecipientType Department
---- ------------- ----------
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b User Sales
b602b148-2fcf-435a-9d34-ce72c3a8c748 UserMailbox Sales
41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 UserMailbox Marketing
c32b2b27-d809-439a-a3e3-eb7a749eeb72 UserMailbox IT
67962421-00e7-448b-b382-83b7b434e41c User Sales UK
6ec79c8e-5a84-4992-b2df-4caf3f997a74 User Spain Sales
5cae3874-442b-459c-8f33-3aee5b879275 UserMailbox Marketing
bc741ead-e704-4eb9-a9e8-adfa1f187c92 User Sales
0de964ae-f09a-4e65-bc8c-cbeac2745e4c User Sales
2beda4b9-559b-4d39-9415-51ce47f2963f User Sales UK
33a56fba-ed21-456d-90be-ade268b9acc8 UserMailbox Sales USA
d23f1e98-02db-4292-98aa-6f511c433f1b User SalesWorldWide
411a8f10-0dfa-4034-a1e3-a8b6e4cad2f6 UserMailbox IT
e5c3a5e8-eeae-4829-94dc-fb7228bcf8da UserMailbox IT
ee042397-eb86-4c54-836a-7879df27ec1c User Brandsales
ca21f458-f33c-46bf-9f57-55c201ccbd82 User Sales USA
4631b453-3a0a-439e-a2ea-7af2b9a741b3 User Information Technology
7545348c-b473-45b5-890d-38bc59b4a163 User Information Technology
0f38d53f-cbe0-4844-86e9-1032a45ba31b UserMailbox MarketingComparison operator -lt
The Equality Comparison operator -lt is the opposite of -gt. When using -lt in PowerShell, you will only get an output if the value on the left is less than the number on the right.
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.DisplayName.Length -lt 8} | Select Name, RecipientType, DisplayNameName RecipientType DisplayName
---- ------------- -----------
394963d9-cab1-4d3c-a99e-7839a2d19690 User MarkComparison operator -le
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.DisplayName.Length -le 8} | Select Name, RecipientType, DisplayNameName RecipientType DisplayName
---- ------------- -----------
Info Box UserMailbox Info Box
954b27cf-8401-420b-bbd2-7f70903c0707 User New User
394963d9-cab1-4d3c-a99e-7839a2d19690 User MarkMatching Comparison operator examples
We will show you different examples using Matching Comparison operators. The PowerShell Matching Comparison operators are case-insensitive by default. You can also use the case-sensitive versions of the Matching operators, which are the same but with the -c prefix, such as -clike, -cnotlike, -cmatch, and -cnotmatch.
Comparison operator -like
- Sales USA
- Sales UK
- Spain Sales
- Brandsales
- SalesWorldWide
- Store sales
- all SALES
The Matching Comparison operator (-like) enables us to locate values that include or have a particular string. Instead of creating two PowerShell sentences, we can create a condition to find all the departments that start with Sales.
In our example, we look for values that start with the string “Sales”. When using the comparison operator (-like), we need to add the asterisk (*) character, which serves as a wildcard.
- Before the letters *Sales to find the values that end with it, like Brandales
- After the letters Sales* to find the values that start with it, like SalesWorldWide
- Before and after the letters *Sales* to find the values that include this exact word

Option 1: Use the asterisk (*) after the letters
Run the below PowerShell command example.
Get-User -ResultSize Unlimited | Where {$_.Department -like "Sal*"} | Select Name, RecipientType, DepartmentName RecipientType Department
---- ------------- ----------
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b User Sales
b602b148-2fcf-435a-9d34-ce72c3a8c748 UserMailbox Sales
67962421-00e7-448b-b382-83b7b434e41c User Sales UK
f8261d51-3df9-4f21-a6b1-533412669c11 User Sales
bc741ead-e704-4eb9-a9e8-adfa1f187c92 User Sales
0de964ae-f09a-4e65-bc8c-cbeac2745e4c User Sales
2beda4b9-559b-4d39-9415-51ce47f2963f User Sales UK
33a56fba-ed21-456d-90be-ade268b9acc8 UserMailbox Sales USA
d23f1e98-02db-4292-98aa-6f511c433f1b User SalesWorldWide
ca21f458-f33c-46bf-9f57-55c201ccbd82 User Sales USAOption 2: Use the asterisk (*) before and after the letters
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.Department -like "*les*"} | Select Name, RecipientType, DepartmentName RecipientType Department
---- ------------- ----------
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b User Sales
b602b148-2fcf-435a-9d34-ce72c3a8c748 UserMailbox Sales
1a1fd41c-ca5f-4432-8313-7a662576856b User Store sales
67962421-00e7-448b-b382-83b7b434e41c User Sales UK
6ec79c8e-5a84-4992-b2df-4caf3f997a74 User Spain Sales
dafbb7ee-6e2d-4786-8180-1dba57095ead User all SALES
f8261d51-3df9-4f21-a6b1-533412669c11 User Sales
7bfec79d-7806-484a-ac83-133cd4cf5af5 User all SALES
bc741ead-e704-4eb9-a9e8-adfa1f187c92 User Sales
0de964ae-f09a-4e65-bc8c-cbeac2745e4c User Sales
2beda4b9-559b-4d39-9415-51ce47f2963f User Sales UK
33a56fba-ed21-456d-90be-ade268b9acc8 UserMailbox Sales USA
d23f1e98-02db-4292-98aa-6f511c433f1b User SalesWorldWide
ee042397-eb86-4c54-836a-7879df27ec1c User Brandsales
ca21f458-f33c-46bf-9f57-55c201ccbd82 User Sales USAOption 3: Use the asterisk (*) before the letters
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.Department -like "*les"} | Select Name, RecipientType, DepartmentName RecipientType Department
---- ------------- ----------
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b User Sales
b602b148-2fcf-435a-9d34-ce72c3a8c748 UserMailbox Sales
1a1fd41c-ca5f-4432-8313-7a662576856b User Store sales
6ec79c8e-5a84-4992-b2df-4caf3f997a74 User Spain Sales
dafbb7ee-6e2d-4786-8180-1dba57095ead User all SALES
f8261d51-3df9-4f21-a6b1-533412669c11 User Sales
7bfec79d-7806-484a-ac83-133cd4cf5af5 User all SALES
bc741ead-e704-4eb9-a9e8-adfa1f187c92 User Sales
0de964ae-f09a-4e65-bc8c-cbeac2745e4c User Sales
ee042397-eb86-4c54-836a-7879df27ec1c User BrandsalesComparison operator -clike
When using the case-sensitive Matching Comparison operators -clike, you can get more specific results in PowerShell. It means that the string (letters or word) you provide will be case-sensitive, meaning you can distinguish between small and capital letters.
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.Department -clike "*LES"} | Select Name, RecipientType, DepartmentName RecipientType Department
---- ------------- ----------
dafbb7ee-6e2d-4786-8180-1dba57095ead User all SALES
7bfec79d-7806-484a-ac83-133cd4cf5af5 User all SALESComparison operator -match
The -match comparison operator is similar to the -like operator. The -match comparison operator uses a regular expression (regex pattern). If you know the word you are looking for, it’s best to use the -match comparison operator because the results are faster.
Use the (^) character to indicate where the string begins or ends:
- Use the character (^) before the word ^Sales to find the values that end with it, like Brandsales
- Use the character (^) after the word Sales^ to find the values that start with it, like SalesWorldWide

Run the below PowerShell command example with the Comparison operator -match.
Get-User -ResultSize Unlimited | Where-Object {$_.Department -match "Sales"} | Select Name, RecipientType, DepartmentComparison operator -cmatch
When using the case-sensitive Matching Comparison operator -cmatch, you can get more specific results in PowerShell.
Get-User -ResultSize Unlimited | Where-Object {$_.Department -cmatch "sales"} | Select Name, RecipientType, DepartmentName RecipientType Department
---- ------------- ----------
1a1fd41c-ca5f-4432-8313-7a662576856b User Store sales
ee042397-eb86-4c54-836a-7879df27ec1c User BrandsalesReplacement Comparison operator examples
We will show you different examples of using Replacement Comparison operators.
Comparison operator -replace
The -replace operator supports regular expressions, so you can customize the pattern and replacement string based on your specific requirements.

- Type the username in line number 2
- Specify the old username in line number 5
- Specify the new username in line number 5
# Get user information
$user = Get-User -Identity "username"
# Use -replace operation
$newDisplayName = $user.DisplayName -replace "old username", "new username"
# Update user display name
Set-User -Identity $user.Identity -DisplayName $newDisplayName- Run the below PowerShell example script
# Get user information
$user = Get-User -Identity "Andrew Marshall"
# Use -replace operation
$newDisplayName = $user.DisplayName -replace "Andrew Marshall", "Andrew"
# Update user display name
Set-User -Identity $user.Identity -DisplayName $newDisplayName- There is no PowerShell output, but you will get a confirmation alert. Press Y followed by enter.
Confirm
Are you sure you want to perform this action?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):Containment Comparison operator examples
We will show you different examples of using Containment Comparison operators.
Comparison operator -contains
- In the first line, we will use the $departments and specify the collection in parenthesis brackets ( ).
- In the second line, we will use the Get-User cmdlet followed by -ResultSize Unlimited.
- In the next part of the PowerShell sentence, we will use the Where cmdlet, followed by a condition in braces brackets { }.
- Then, we use the specified element $departments followed by the comparison operator -contains to see if it includes the collection property $_.Department.
- Lastly, we use the shortened version of Select-Object to Select the properties we want to display.
Run the below PowerShell example.
$departments = @("Sales", "Marketing", "IT")
Get-User -ResultSize Unlimited | Where {$departments -contains $_.Department} | Select Name, RecipientType, DepartmentThe PowerShell output result.
Name RecipientType Department
---- ------------- ----------
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b User Sales
b602b148-2fcf-435a-9d34-ce72c3a8c748 UserMailbox Sales
41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 UserMailbox Marketing
c32b2b27-d809-439a-a3e3-eb7a749eeb72 UserMailbox IT
f8261d51-3df9-4f21-a6b1-533412669c11 User Sales
5cae3874-442b-459c-8f33-3aee5b879275 UserMailbox Marketing
bc741ead-e704-4eb9-a9e8-adfa1f187c92 User Sales
0de964ae-f09a-4e65-bc8c-cbeac2745e4c User Sales
411a8f10-0dfa-4034-a1e3-a8b6e4cad2f6 UserMailbox IT
e5c3a5e8-eeae-4829-94dc-fb7228bcf8da UserMailbox IT
27c03114-f9a1-4c02-83ef-bd4ee32f00cf User Sales
82cd0d62-e974-4892-aca6-e0387abc62be UserMailbox Sales
52a6c1c7-77d2-4109-99b9-a4076167b6e2 UserMailbox Sales
0f38d53f-cbe0-4844-86e9-1032a45ba31b UserMailbox MarketingComparison operator -in
Run the below PowerShell command example.
$departments = @("Sales", "Marketing", "IT")
Get-User -ResultSize Unlimited | Where {$_.Department -in $departments} | Select Name, RecipientType, DepartmentType Comparison operator examples
We will show you an example using the Type Comparison operators.
Comparison operator -is
In our example, we want to check if a value is a string.
Run the below PowerShell command example.
$value = "Sales and Marketing"
if ($value -is [string]) { Write-Host "The value is a string!"
}
else { Write-Host "The value is not a string."
}The PowerShell output shows that the value (Sales and Marketing) is a string.
The value is a string!Comparison operator -isnot
Use the Type Comparison operator -isnot to check if an object is not a specific type.
The PowerShell syntax command.
"object" -isnot "type-reference"In our example, we want to check if a number is not an integer.
Run the below PowerShell example.
42 -isnot [int]The PowerShell output shows True or False.
FalseCombine PowerShell Comparison operators and Logical operator examples
We will show you different examples of using Equality Comparison operators with Logical operators.
Comparison operators -eq -ne with Logical operator -and
You can use the logical operator -and to combine two or more conditions in a PowerShell command.

Run the PowerShell command example.
Get-User -ResultSize Unlimited | Where {($_.Department -eq "Sales") -and ($_.StateOrProvince -ne "Texas")} | Select Name, RecipientType, Department, StateOrProvinceName RecipientType Department StateOrProvince
---- ------------- ---------- ---------------
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b User Sales Ontario
b602b148-2fcf-435a-9d34-ce72c3a8c748 UserMailbox Sales Ontario
f8261d51-3df9-4f21-a6b1-533412669c11 User Sales Washington
bc741ead-e704-4eb9-a9e8-adfa1f187c92 User Sales Florida
0de964ae-f09a-4e65-bc8c-cbeac2745e4c User Sales New YorkComparison operators -like -eq with Logical operator -or

- In the first part of the PowerShell sentence, we will use the Get-User followed by -ResultSize Unlimited.
- In the second part of the PowerShell sentence, we will use the Where cmdlet, followed by two conditions in braces brackets { }.
- For each condition, you need to use parenthesis brackets ( ).
- The first condition will use the comparison operator -like to match users that start with the letters Sal department.
- After the first condition, we will use the logical operator -or.
- The second condition will use the comparison operator -eq to match users from the Marketing department.
Run the PowerShell command syntax.
Get-User -ResultSize Unlimited | Where {($_.Department -like "Sal*") -or ($_.Department -eq "Marketing")} | Select Name, RecipientType, Department, TitleName RecipientType Department Title
---- ------------- ---------- -----
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b User Sales
b602b148-2fcf-435a-9d34-ce72c3a8c748 UserMailbox Sales
41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 UserMailbox Marketing Leader
67962421-00e7-448b-b382-83b7b434e41c User Sales UK
f8261d51-3df9-4f21-a6b1-533412669c11 User Sales Manager
5cae3874-442b-459c-8f33-3aee5b879275 UserMailbox Marketing
bc741ead-e704-4eb9-a9e8-adfa1f187c92 User Sales
0de964ae-f09a-4e65-bc8c-cbeac2745e4c User Sales Manager
2beda4b9-559b-4d39-9415-51ce47f2963f User Sales UK
33a56fba-ed21-456d-90be-ade268b9acc8 UserMailbox Sales USA
d23f1e98-02db-4292-98aa-6f511c433f1b User SalesWorldWide
ca21f458-f33c-46bf-9f57-55c201ccbd82 User Sales USA
27c03114-f9a1-4c02-83ef-bd4ee32f00cf User Sales
82cd0d62-e974-4892-aca6-e0387abc62be UserMailbox Sales Member
52a6c1c7-77d2-4109-99b9-a4076167b6e2 UserMailbox Sales
0f38d53f-cbe0-4844-86e9-1032a45ba31b UserMailbox Marketing ManagerComparison operators -like -match -eq with Logical operators -and -or

- For the first condition, we will use the comparison operator -like enclosed in parenthesis brackets ( ).
- For the second condition, we will use the comparison operator -match enclosed in parenthesis brackets ( ).
- For the third condition, we will use the comparison operator -eq enclosed in parenthesis brackets ( ).
- The first logical operator -or shows the users from the department that start with Sal* or Marketing department.
- The second logical operator -and shows the users should also be from the state of Ontario.
Run the below PowerShell command example.
Get-User -ResultSize Unlimited | Where {($_.Department -like "Sal*") -or ($_.Department -match "Marketing") -and ($_.StateOrProvince -eq "Ontario")} | Select Name, RecipientType, Department, StateOrProvince Name RecipientType Department StateOrProvince
---- ------------- ---------- ---------------
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b User Sales Ontario
b602b148-2fcf-435a-9d34-ce72c3a8c748 UserMailbox Sales Ontario
5cae3874-442b-459c-8f33-3aee5b879275 UserMailbox Marketing OntarioConclusion
You learned how to use comparison operators in PowerShell to make your commands more efficient. With the comparison operators, you can make shorter scripts that will look more organized.

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:
| Operator | Description |
|---|---|
| -eq | Equal to |
| -ne | Not equal to |
| -gt | Greater than |
| -ge | Greater than or equal to |
| -lt | Less than |
| -le | Less than or equal to |
| -like | Wildcard comparison |
| -notlike | Negative wildcard comparison |
| -match | Regular expression comparison = |
| -notmatch | Negative regular expression comparison |
| -contains | Containment operator |
| -notcontains | Negative containment operator |
| -in | Checks if a value is in a set of values |
| -notin | Checks if a value is not in a set of values |
| -is | Returns True if the object on its left-hand side is of the type specified on its right-hand side. |
| -isnot | Returns True if the object on its left-hand side is not of the type specified on its right-hand side. |
| -replace | replaces 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:
- Checking if a file exists before performing an operation.
- Validating user input against predefined values or patterns.
- Filtering and manipulating data based on specific criteria.
- Comparing timestamps to determine the age of files or folders.
- Testing and validating conditions in control flow statements, such as loops and conditional statements.
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.



