top of page

PowerShell Comparison Operators

Explore the essentials of PowerShell comparison operators with our comprehensive guide. Our website provides detailed tutorials, practical examples, and expert tips on mastering comparison operators in PowerShell. Whether you're new to scripting or looking to refine your skills, our resources will empower you to efficiently compare values, filter data, and optimize your scripts. Start learning today to enhance your PowerShell proficiency and streamline your automation processes with confidence!

Return Key

In many programming languages, standard comparison operators such as ==, !=, <, >, <=, and >= are commonly used to perform equality and relational comparisons. These operators are intuitive and widely recognized across various programming environments, including languages like C, C++, Java, JavaScript, and Python. However, Microsoft PowerShell diverges from this convention by adopting a distinct set of comparison operators that are more verbose and self-descriptive.

​

In PowerShell, instead of using the typical == and != for equality and inequality checks, it uses -eq and -ne, respectively. Similarly, for greater than and less than comparisons, PowerShell uses -gt and -lt. The operators for greater than or equal to and less than or equal to are -ge and -le. This approach aims to enhance readability by making the purpose of each operator more explicit.

​

  • Equality:

    • Standard: ==

    • PowerShell: -eq

  • Inequality:

    • Standard: !=

    • PowerShell: -ne

  • Greater Than:

    • Standard: >

    • PowerShell: -gt

  • Less Than:

    • Standard: <

    • PowerShell: -lt

  • Greater Than or Equal To:

    • Standard: >=

    • PowerShell: -ge

  • Less Than or Equal To:

    • Standard: <=

    • PowerShell: -le

Left: PowerShell Vs Other Languages

The motivation behind PowerShell's unique operators is to ensure that scripts are self-explanatory, particularly for system administrators who might not have a strong background in traditional programming languages. By using operators like -eq and -ne, the intent of the comparison is immediately clear, reducing the likelihood of confusion and errors.

​

Additionally, PowerShell extends its comparison operators to handle more complex conditions with operators such as -like for wildcard pattern matching, -match for regular expressions, and -contains for checking membership in collections. These operators further enrich the language's expressiveness and align with its goal of providing powerful and readable scripting capabilities.

​

While this deviation from standard comparison operators can pose a learning curve for those coming from other programming backgrounds, it ultimately contributes to PowerShell's goal of being a clear, efficient, and powerful scripting language tailored for administrative tasks and automation in Windows environments.

 PowerShell Conditional Operators

  • -eq (Equal To)

  • -ceq (for case-sensitive Compare)

  • -ne (not equal to)

  • -lt (less than)

  • -le (less than or equal to)

  • -gt (greater than)

  • -ge (greater than or equal to)

  • -like (like—a wildcard comparison)

  • -notlike (not like—a wildcard comparison)

  • -contains (contains the specified value)

  • -notcontains (doesn't contain the specified value)

  • -creplace (for case-sensitive replace)

Data types used for comparison:
[string] Fixed-length string of Unicode characters
[char] A Unicode 16-bit character
[byte] An 8-bit unsigned character

[int] 32-bit signed integer
[long] 64-bit signed integer
[bool] Boolean True/False value

[decimal] A 128-bit decimal value
[single] Single-precision 32-bit floating point number
[double] Double-precision 64-bit floating point number
[DateTime] Date and Time

[xml] Xml object
[array] An array of values
[hashtable] Hashtable object

Comparison Logical Operators Boolean.

  • -and

  • -or

  • -not

  • !

  • -is

  • -isnot

# Example: -Contains  -NotContains

$Array=@()
$Array=Get-ADuser -filter * | Select -ExpandProperty Name

If($Array -contains "SomeAccount"){
    cls
    Write-Host "Account Name Found" 
}Else{
    cls
    Write-Host "Account match not found"
}

# Example:  Boolean Comparison

$String1 = "I am a String"

$Number = 4

if (($String1 -is [String]) -and ($Number -is [Int])){       

     write-Host "Is a String and a Integer"}Else{     

     write-Host "Is Not a String or Not a Number"

}

bottom of page