The type operators are used to find or change the type of an object in Windows PowerShell. Table 1 lists the type operators available.
Table 1. Windows PowerShell Type Operators
Operator | Description |
---|
-is | Checks if an object is a specified type |
-isnot | Checks if an object is not a specified type |
-as | Converts an object to a specified type |
You can test if an object is a specific type by using the -is operator.
PS > "Hello" -is [System.String]
True
PS > "Hello" -is [System.Int32]
False
In the first example, we check if "Hello" is of the type System.String, which returns True. In the second example, we check if "Hello" is of the type System.Int32, which returns False.
To make sure that an object is not a specific type, use the -isnot operator.
PS > "Hello" -isnot [System.String]
False
PS > "Hello" -isnot [System.Int32]
True
You can convert objects to a specified type using the -as operator.
PS > 1.123 -as [System.Int32]
1
In this example, we convert the numeric value 1.123 to an object of the type System.Int32.