CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Type operators

PowerShell Type operators are used to verify the type of the operand. Are you aware, each value we use in PowerShell has particular Type? These are the types similar to the types defined in .Net. Yes, because PowerShell is developed using .Net Framework.

These operators are allowed to verify the type of the operand. Through this Article, we are going to discuss usage of these operators.

The -is operator

This operator verifies whether the given operand is of specific type. The operand should be left side of the operator and the verified type should be mentioned in right hand side of the operator, enclosed in quotes (single (‘) or double (“) quotes).

It verifies the operand, and returns True , if the operand is of mentioned type; otherwise, it returns False as the return value.

For example, below statement verifies whether the string is of String type; 

PS C:\> "PowerShell" -is "String"
True

Let’s verify the same with other type;

PS C:\> "PowerShell" -is "Object"
True

Why it showing True, when we compare the string with Object type? Yes. Everything is derived from an Object class in .Net; string is also of Object type; hence the above statement returns the value True.

The -isNot operator

This operator works exactly opposite to -is operator. It verifies whether the operand is NOT, of mentioned type. It also returns; True or False as the return value.

PS C:\> 1234 -isNot "Int32"
False

The -as operator

This is interesting operator. It type casts from one type to another type. Let’s verify this with below example;

PS C:\> 1234 -is "String"
False
PS C:\> (1234 -as "String") -is "String"
True

Observe that, in first statement we did the type check; whether the number is of string type or not. The -is operator, returns False. And in the second statement, we type casts the number to string and verifies whether it is of type string; and it succeeds the check.

Does this check will succeed, always? Not necessarily. For example, when you attempt to type cast a string type to a numeric type, it will thrown an error, like below;

PS C:\> ("1680" -as 1680)
Cannot convert the "1680" value of type "System.String" to type "System.Type".
At line:1 char:1
+ ("1680" -as 1680)
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToType

So far, so good. But, how do we know the variable holds, what type of information? Good. We can check this through GetType() method.

The GetType() method

This method returns the type information of the expression. By calling this method, we will come to know; what type the variable or value or the expression is. When the variable is declared, the value must be assigned to it; then only the type information will be captured. Otherwise, the variable is assigned with a null value; and when we call this method; PowerShell will throw the Error.

Here is an example, where it throws the InvalidOperation error;

PS C:\> $var
PS C:\> $var.GetType()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $var.GetType()
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Now, we assign a value to the variable $var; and call the method GetType() on it, to get the type information. The result looks like, below;

PS C:\> $var = (1, 2, 3)
PS C:\> $var.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

Type information is really useful when dealing with different types of data.

[..] David

PowerShell – Type operators

Leave a Reply

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

Scroll to top