Comparison Operators
You can use many comparison operators to specify or identify a condition. Comparison operators compare two values to determine whether a condition exists. Most comparison operators return True if the condition exists and a False if it doesn't exist.
As a simple example, imagine a variable called $num had a value of 100. The comparison $num -eq 100 would be evaluated as True, while $num -eq 5 would be evaluated as False.
Comparisons don't have to be numbers. They can also be text; however, when comparing text data, the text string needs to be enclosed in quotes. For example, if you wanted to know if a variable named $str has a value of "MCITP," you'd use this comparison:
$str -eq "MCITP"
String comparisons are case insensitive by default. In other words, both of these would evaluate to True:
$str -eq "mcitp" $str -eq "MCITP"
However, if you want the comparison to be case sensitive, you can add the letter c to the operator switch, like this:
$str -ceq "True"
Table below lists some of the commonly used comparison operators. You can enter these at the command line to see the result. For example, to see how the -eq command works, you could populate the variable $num with 5 and then use the variable in a comparison like this:
$num = 5 $num -eq 100
PowerShell will return False.
Comparison operators
Operator | Description |
-eq | Equals, as in $x -eq 100 or $x -eq "y" |
-ne | Not equal, as in $x -ne 100 or $x -ne "y" |
-gt | Greater than, as in $x -gt 100 |
-ge | Greater than or equal to, as in $x -ge 100 |
-lt | Less than, as in $x -lt 100 |
-le | Less than or equal to, as in $x -le 100 |
-like | Compares strings using the wildcard character * and returns True if a match
is found. The wildcard character can be used at the beginning, middle, or end
to look for specific matches. If a variable named $str holds the string "MCITP
Windows 7," then all of the following comparisons will return True:
$str -like "MCITP*" $str -like "*Win*" $str -like "*7" |
-notlike | Compares strings using the wildcard character * and returns True if the match is not found. |
You'll see comparison operators used in many different ways. For example, they are used in cmdlet switches when a comparison is needed and in IF statements when you're scripting.
In this tutorial:
- Working with the Command Prompt
- Starting and Ending a Command Prompt Session
- Easy ways to invoke administrator Command Prompt sessions
- Starting Command Prompt at a Particular Folder
- Strings with Spaces Need Quotes
- Cmd.exe vs. Command.com
- Commands Are Not Case Sensitive
- Starting Command Prompt and Running a Command
- Cmd.exe and Other Command Prompts
- Using AutoRun to Execute Commands When Command Prompt Starts
- Using Cmds Command-Line Syntax
- Using Commands
- Type /? for help
- Starting Programs
- Open Windows Explorer at the current Command Prompt folder
- Using File-Name and Folder-Name Completion
- Use a different completion character
- Using Wildcards
- Editing the Command Line
- Using Command Symbols
- The Redirection Symbols
- The Pipe Symbol
- The Command Combination Symbols
- Pausing or Canceling Commands
- Simplifying Command Entry with Doskey Macros
- DOSKEY Saves Typing
- System Variables Identify the Environment
- Viewing Environment Variables
- Modifying Environment Variables
- Predefined Environment Variables
- Customizing Command Prompt Windows
- Setting the Window Size and Position
- Setting the Window Size and Position Visually
- Selecting a Font
- Setting Colors
- Setting Other Options
- Copy and paste in the command prompt window
- Navigating from the command prompt
- Printing a list of filenames
- Commands Use Paths
- Identifying Executables
- Modifying the Path to Executables
- Modifying the Path with the GUI
- Changing the Current Path with CD
- Changing the Current Path with Windows Explorer
- Capturing the Output
- A Sampling of Commands
- Dir
- Copy
- XCopy
- SET
- NET USE
- SystemInfo
- DriverQuery
- Echo
- Advanced Shell Commands
- Creating a Batch File
- Scheduling a Batch File
- Creating Scheduled Tasks with a Script
- Using Windows PowerShell and the PowerShell ISE
- Windows PowerShell ISE
- PowerShell Commands
- Verbs and Nouns
- Sending Output to a Text File
- PowerShell Syntax
- Variables Created with a $ Symbol
- Comparison Operators
- Parentheses, Brackets, and Braces
- Running PowerShell Scripts
- PowerShell Execution Policy
- Changing the Execution Policy
- Looping
- Collections
- Creating a PowerShell Script
- Documenting Scripts
- Using PowerShell Commands
- Getting Help on PowerShell
- Using WMI_Cmdlets
- Getting Details on an Object
- Querying Information on Specific Objects
- Terminate Applications with Win32_process
- Formatting Output with the -f Format Operator
- Filtering the Output with the Where-Object Command
- Using the IF statement
- Using the Switch Statement
- Script Reusability