Coding Ref

Not equal to in PowerShell

Not equal to in PowerShell

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

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

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

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

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

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

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

Since the string Hello and the integer 10 are not equal, the -ne operator will return $true and the code will output The values are not equal.

Conclusion

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

You'll also like

Related tutorials curated for you

    What is Write-Host in PowerShell?

    What is WhatIf in PowerShell?

    How to declare global variables in PowerShell

    What is PowerShell logging?

    How to use If Else in PowerShell

    How to use String Contains in PowerShell

    How to count objects in PowerShell

    How to ping in PowerShell

    How to find the Windows version from the PowerShell command line

    What is null in PowerShell?

    How to sort in PowerShell

    How to wait in PowerShell