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.
Matching 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, Department
Name 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 USA
Option 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, Department
Name 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 USA
Option 3: Use the asterisk (*) before the letters
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.Department -like "*les"} | Select Name, RecipientType, Department
Name 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 Brandsales
Comparison 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, Department
Name RecipientType Department
---- ------------- ----------
dafbb7ee-6e2d-4786-8180-1dba57095ead User all SALES
7bfec79d-7806-484a-ac83-133cd4cf5af5 User all SALES
Comparison 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, Department
Comparison 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, Department
Name RecipientType Department
---- ------------- ----------
1a1fd41c-ca5f-4432-8313-7a662576856b User Store sales
ee042397-eb86-4c54-836a-7879df27ec1c User Brandsales
Equality 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, RecipientType
Name 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, Department
Name 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 Sales
Comparison 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, StateOrProvince
Name 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 Texas
Comparison 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, Department
Name 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 Marketing
Comparison operator -ge
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.Department.Length -ge 2} | Select Name, RecipientType, Department
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
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 Marketing
Comparison 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, DisplayName
Name RecipientType DisplayName
---- ------------- -----------
394963d9-cab1-4d3c-a99e-7839a2d19690 User Mark
Comparison operator -le
Run the below PowerShell command.
Get-User -ResultSize Unlimited | Where {$_.DisplayName.Length -le 8} | Select Name, RecipientType, DisplayName
Name RecipientType DisplayName
---- ------------- -----------
Info Box UserMailbox Info Box
954b27cf-8401-420b-bbd2-7f70903c0707 User New User
394963d9-cab1-4d3c-a99e-7839a2d19690 User Mark
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, Department
The 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 Marketing
Comparison operator -in
Run the below PowerShell command example.
$departments = @("Sales", "Marketing", "IT")
Get-User -ResultSize Unlimited | Where {$_.Department -in $departments} | Select Name, RecipientType, Department
Connect to Exchange Online PowerShell
Connect-ExchangeOnline
Combine 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, StateOrProvince
Name 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 York
Comparison 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, Title
Name 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 Manager
Comparison 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 Ontario
PowerShell 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, Property2
Replacement 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"):
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 |
Type 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.
False
Conclusion
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.