PowerShell Type Operators

In this tutorial we will see about PowerShell Type Operators. Thee operators are able to check if a value is a specific type of data or try to change the data to the specific type. There are only three PowerShell Type Operators.

Below is the list of the operators:

-is – Returns TRUE when the input is an instance of the specified .NET type.

– Returns TRUE when the input is an instance of the specified .NET type. -isnot – Returns TRUE when the input not an instance of the specified.NET type.

– Returns TRUE when the input not an instance of the specified.NET type. -as – Converts the input to the specified .NET type.

-is and -isnot Operators

These type operators are Boolean operators. As they check the type of data for the input with the .NET Framework type, they will return TRUE or FALSE. The -is operator will return TRUE if the data type of the input is an instance of the specified .NET Framework type. The -isnot operator will return TRUE if the date type of the input is not an instance of the specified .NET Framework type. Otherwise, the operators will return FALSE. Lets see some examples to better understand.

[adinserter name=”In Article”]

Example 1

In our first example we will see how we are able to use this two type operators. At the first part we are checking the result of Get-Date command is the data type is System.DateTime . On the next parts we check the data type a variable that has stored the information of a file. As you will see below, we will check the data type against different .NET Framework types.

Code:

(Get-Date) -is [System.DateTime] $a = Get-Item 'C:\Scripts_Output\file1.txt' $a -is [System.IO.FileSystemInfo] $a -is [System.IO.FileInfo] $a -is [System.IO.DirectoryInfo] $a = Get-Item 'C:\Scripts_Output\file1.txt' $a -isnot [System.IO.FileSystemInfo] $a -isnot [System.IO.FileInfo] $a -isnot [System.IO.DirectoryInfo]

Output:

As you may have noticed in the output, when we are testing the data type with System.IO.FileSystemInfo and System.IO.FileInfo , we are getting the same result. This is because System.IO.FileSystemInfo is the base class of System.IO.FileInfo . System.IO.FileSystemInfo is the base class for both System.IO.FileInfo and System.IO.DirectoryInfo . So, if you will check the data type of a directory against System.IO.FileSystemInfo and System.IO.DirectoryInfo , you will get the same result for both.

[adinserter name=”In Article”]

Find the data type

There are two methods to find the .Net Framework type of an object. You can either use the GetType() method or use the cmdlet Get-Member . GetType() will only show you the type of the object. If you will use Get-Member cmdlet, it will provide you not only with the data type of the object but it will also provide you with the properties and methods of the object along with the data type of each one. Lets see below in our example, how we are able to use those two methods.

Example 2

Code:

$a = Get-Item 'C:\Scripts_Output\file1.txt' $a.GetType() $a | Get-Member

Output:

There are some times while we are writing our scripts, after the processing of the data, we type might be not what we expect. The results sometimes are not what it suppose too and this may be due to the data type of some of our objects. With the methods above you can find out what is the data type of an object. If the data type is not as expected, we may try to convert the data type to the one that we need, by using the -as operator.

[adinserter name=”In Article”]

-as Operator

The -as operator can be used to convert the input object into the .NET Framework type specified. If the conversion is successful, then it will return the object. If the conversion will fail then nothing will be return (no error is returned). Let’s see below some examples in order to understand better.

Example 3

Code:

$a = 5 $a.GetType() $b = "5" $b.GetType() $c = $a -as [String] $c.GetType() $d = $b -as [Int32] $d.GetType() $a + $b $b + $a $c = $a -as [Int32] $c + $d $d + $c

Output:

As you can see when we use + Arithmetic Operator we get different results based on the order that we are using them. When $a is front, the type of $a is [Int32] , so the + is performing addition. When $b is in front, which is [String] , then the + operator will only combines the values as strings. You can also see above the we have converted the types to different ones using the -as operator. In order to get the same result with the + operator we have converted both types to [Int32] so they are always be processed as numbers and the result will be 10. If we would have converted them as [String] then they would be processes as stings and the result will always be 55.

[adinserter name=”In Article”]

Find all .NET Framework types

If you want to find all .Net Framework types that are loaded in the your current session you need to use Get-TypeData . The Get-TypeData cmdlet gets the extended type data in the current session. This includes type data that was added to the session by Types.ps1xml file and dynamic type data that was added by using the parameter of the Update-TypeData cmdlet. For more information about Types.ps1xml please check the link in the next section.

Code:

Get-TypeData

Output:

I hope the tutorial about PowerShell Type Operators is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

[adinserter name=”Matched-Content”]