Some time ago I received the question what the operators ‘-eq’, ’-lt’ and ‘-ne’ mean, what other operators they are and whatever else I could tell about them. I think this is a good topic for a blog post since most beginning PowerShell scripters don’t know which operators are available to them… and for your convenience I’ve provided some real-life examples 
There are two kinds: Comparison and logical operators.
Comparison Operators
These operators are used to compare values to conditions you specify. There are a bunch of operators in this category, so let’s name and explain them one at a time:
Operator |
-eq |
Name: |
Equal to. |
Description: |
Used to compare an identical value. |
Example: |
Get-childitem | where {$_.Name –eq “PowerShell notes”} |
Explanation: |
Will find all files where the name of the file is equal to “PowerShell notes”. |
Operator |
-ne |
Name: |
Not equal to. |
Description: |
Used to compare a different value. |
Example: |
Get-childitem | where {$_.Name –ne “PowerShell notes”} |
Explanation: |
Will find all files where the name is not equal to “PowerShell notes”. |
Operator |
-gt |
Name: |
Greater than |
Description: |
Used to compare a different value in size. |
Example: |
Get-childitem | where {$_.Length –gt 1MB} |
Explanation: |
Will find all files where the size is greater than 1MB. |
Operator |
-ge |
Name: |
Greater than or equal to |
Description: |
Used to compare a different or identical value in size. |
Example: |
Get-childitem | where {$_.Length –ge 1MB} |
Explanation: |
Will find all files where the size is equal to or greater than 1MB. |
Operator |
-lt |
Name: |
Less than |
Description: |
Used to compare a different value in size. |
Example: |
Get-childitem | where {$_.Length –lt 1MB} |
Explanation: |
Will find all files where the size is less than 1MB. |
Operator |
-le |
Name: |
Less than or equal to |
Description: |
Used to compare a different or identical value in size. |
Example: |
Get-childitem | where {$_.Length –le 1MB} |
Explanation: |
Will find all files where the size is equal to or less than 1MB. |
Operator |
-like |
Name: |
Like |
Description: |
Match an identical value using a wildcard character. |
Example: |
Get-childitem | where {$_.Name –like “PowerShell*”} |
Explanation: |
Will find all files where the name begins with “PowerShell”. |
Operator |
-notlike |
Name: |
Not like |
Description: |
Does not match an identical value using a wildcard character. |
Example: |
Get-childitem | where {$_.Name –notlike “PowerShell*”} |
Explanation: |
Will find all files where the name does not begin with “PowerShell”. |
Operator |
-match |
Name: |
Match |
Description: |
Used to compare a string using a regular expression. |
Example: |
Get-childitem | where {$_.Name –match “Power”} |
Explanation: |
Will find all files where the name contains “Power”. |
Operator |
-notmatch |
Name: |
Not match |
Description: |
Used to compare a string using a regular expression. |
Example: |
Get-childitem | where {$_.Name –notmatch “Power”} |
Explanation: |
Will find all files where the name does not contain “Power”. |
Operator |
-contains |
Name: |
Contains |
Description: |
Used to see if all of the objects that are provided contain a specific value. |
Example: |
“Microsoft”, “Windows”, “PowerShell” –contains “Power” |
Explanation: |
Looks if the string “Power” is included in all of the provided objects and therefor it will answer “False” to that question. |
Operator |
-notcontains |
Name: |
Not contains |
Description: |
Used to see if all of the objects that are provided do not contain a specific value. |
Example: |
“Windows”, “PowerShell” -notcontains “Power” |
Explanation: |
Looks if the string “Power” is not included in all of the provided objects and therefor it will answer “True” to that question. |
Operator |
-replace |
Name: |
Replace |
Description: |
Used to change the value of an element. |
Example: |
Get-childitem –include *.doc | where {$_ –replace “.doc”, “.docx”} |
Explanation: |
Will find all *.doc files and changes their extension from .doc to .docx |
Note: |
This command only changed the extension, it does not convert the file to a new format! |
Logical Operators
These operators are used to connect multiple expressions and/or statements to each other. This will allow you to use a one expression and test them against multiple conditions:
Operator |
-and |
Name: |
And |
Description: |
Logical “and”. Only true when both statements are true. |
Example: |
Get-childitem | where {($_.Length -gt 1MB ) -and ($_.Length -lt 10MB ) } |
Explanation: |
Will find all files where the size is between 1MB and 10 MB. |
Operator |
-or |
Name: |
Or |
Description: |
Logical “or”. Only true when either or both statements are true. |
Example: |
Get-childitem | where {($_.Name –like “*Microsoft*” ) –or ($_.Name –like “*Citrix*” ) } |
Explanation: |
Will find all files where the name includes either “Microsoft” or “Citrix”. |
Operator |
-xor |
Name: |
Exclusive or |
Description: |
Logical “exclusive or”. Only true when one of the statements is true and the other is false. |
Example: |
Get-childitem | where {($_.Name –like “*Microsoft*” ) –xor ($_.Name –like “*RDS*” ) } |
Explanation: |
Will find all files where the name includes “Microsoft” but does not include “RDS”. |
Operator |
-not |
Name: |
Not |
Description: |
Logical “not”. Negates the statement that follows. |
Example: |
Get-childitem | where {($_.Name –like “*Microsoft*” ) –and –not ($_.Length –gt 1MB ) } |
Explanation: |
Will find all files where the name includes “Microsoft” and the file is not greater than 1MB. |
Operator |
! |
Name: |
Not |
Description: |
The same as operator –not |
Example: |
Get-childitem | where {($_.Name –like “*Microsoft*” ) –and !($_.Length –gt 1MB ) } |
Explanation: |
The same as operator –not |
Dear Jeff,
example for contains/notcontains is wrong! This does compare if the given element is in a sequence. It does not compare if it is a substring of a string in the sequence.
“Windows”, “PowerShell” -contains “Power” eqals to $false. Perhaps you have made copy and paste error, because in -notcontains you wrote “Shell” and in comment “Power”
Regards
Markus
Hi Markus,
Good call!
I’ve edited the post, thanks.
Jeff.