Power Shell – Type comparison

What is comparison Operator?

Comparison operators let you specify conditions for comparing values and finding values that match specified patterns. To use a comparison operator, specify the values that you want to compare together with an operator that separates these values.

Type comparison

The type comparison operators (-is and -isnot) are used to determine if an object is a specific type.

Operators  Description
-is Returns true if both object are the same type
-isnot Returns true if the objects are not the same type

-is Operator

Syntax: <object> -is <type reference>

Example:

PS C:\>
$stringMsg1 = "dotnet-helpers"
$stringMsg2 = 100
$stringMsg1 -is $stringMsg2.GetType()
$stringMsg2 -is [int]

OUTPUT

False
True

-isnot Operator

Syntax: <object> -is <type reference>

Example:

PS C:\>
$stringMsg1 = "dotnet-helpers"
$stringMsg2 = 100
$stringMsg1 -isnot $stringMsg2.GetType()
$stringMsg2 -isnot [int]

OUTPUT

True
False

2 thoughts on “Power Shell – Type comparison”

  1. Hi David,

    The left hand side value always be an object (-is of a type). please find the below syntax.

    Syntax:  -is 
    
    Correct Example : “dfklj” -is “lsdkjf”.gettype()
    
  2. shouldn’t this return true? But I get false

    “dfklj”.gettype() -is “lsdkjf”.gettype()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.