Coding Ref

How to check for equality in PowerShell

How to check for equality in PowerShell

In PowerShell, you can check if two values are equal using the -eq operator. This operator returns $true if the values on either side of it are equal, and $false if they are not equal.

Here's an example of using the -eq operator to check if two values are equal:

$value1 = 10
$value2 = 10
if ($value1 -eq $value2) {
Write-Output "The values are equal"
} else {
Write-Output "The values are not equal"
}

In this example, the -eq operator is used to compare the values of $value1 and $value2. Since $value1 is 10 and $value2 is 10, the -eq operator will return $true and the code will output The values are equal.

You can also use the -eq operator to compare the values of different types. For example:

$value1 = "Hello"
$value2 = "Hello"
if ($value1 -eq $value2) {
Write-Output "The values are equal"
} else {
Write-Output "The values are not equal"
}

In this example, the -eq operator is used to compare the values of $value1 and $value2, which have different types (string and string).

Since the string Hello is equal to the string Hello, the -eq operator will return $true and the code will output The values are equal.

Conclusion

The -eq operator is a useful tool for checking if two values are equal in PowerShell.

You'll also like

Related tutorials curated for you

    How to get processes in PowerShell

    What are verbs in PowerShell?

    What is `ls` for PowerShell?

    How to print output in PowerShell

    How to sort and filter data in PowerShell

    How to run a .bat file in PowerShell

    How to use Robocopy in PowerShell

    How to append content to a file in PowerShell

    String Interpolation in PowerShell

    What is IEX in PowerShell?

    Add-Content in PowerShell

    How to move items in PowerShell