• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Type Operators

PowerShell Type Operators

03/08/2018 by Stephanos Leave a Comment

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.
  • -isnot – 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:

PowerShell Type Operators - Example 1

 

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:

PowerShell Type Operators - Example 2

 

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:

PowerShell Type Operators - Example 3

 

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:

PowerShell Type Operators - Example 4

 

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:

  • PowerShell Tutorials
  • PowerShell Scripts
  • Get-Date – Microsoft Docs
  • Get-Member – Microsoft Docs
  • Get-Item – Microsoft Docs
  • PowerShell Arithmetic Operators
  • Get-TypeData – Microsoft Docs
  • Update-TypeData – Microsoft Docs
  • about_Types.ps1xml | Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Type Operators
Article Name
PowerShell Type Operators
Description
PowerShell Type Operators. In this tutorial you will find information about PowerShell Type Operators and their use. Stephanos Constantinou Blog
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Get-Date, Get-Item, Get-Member, Get-TypeData, Update-TypeData

Reader Interactions

Leave a Reply Cancel 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.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact