Equal and not equal comparison
The most basic and most used comparison operator is equal to (-eq
). This operator is flexible in nature as it can be used for strings, integers, and objects. The -eq
operator is used by calling value1
is equal to value2
. When the –eq
operator evaluates the statement, it will return a Boolean value of either True
or False
. If the expression evaluates to be True
, PowerShell will continue to proceed to execute the code.
A script that shows how to use equal comparison operators would look like this:
$value1 = "PowerShell" $value2 = "PowerShell" if ($value1 –eq $value2) { Write-Host "It's Equal!" }
The output of this is shown in the following screenshot:
From the preceding example, you will see that the equal comparison operator determines that $value1
is equal to $value2
and it writes to the screen It's Equal!
. In the instance that you want to determine whether two values are not equal, you can use the –...