Reader small image

You're reading from  Mastering PowerShell Scripting - Fourth Edition

Product typeBook
Published inJun 2021
PublisherPackt
ISBN-139781800206540
Edition4th Edition
Right arrow
Author (1)
Chris Dent
Chris Dent
author image
Chris Dent

Chris Dent is an automation specialist with deep expertise in the PowerShell language. Chris is often found answering questions about PowerShell in both the UK and virtual PowerShell user groups. Chris has been developing in PowerShell since 2007 and has released several modules over the years.
Read more about Chris Dent

Right arrow

Type operators

Type operators are designed to work with and test .NET types. The following operators are available:

  • As: -as
  • Is: -is
  • Is not: -isnot

These operators may be used to convert an object of one type into another, or to test whether or not an object is of a given type.

As

The -as operator is used to attempt to convert a value into an object of a specified type. The operator returns null (without throwing an error) if the conversion cannot be completed.

For example, the operator may be used to perform the following conversions:

"1" -as [Int32] 
'String' -as [Type] 

If the attempt to convert the value fails, nothing is returned and no error is raised:

$true -as [DateTime]

The -as operator can be useful for testing whether a value can be cast to a specific type, or whether a specific type exists.

For example, the System.Windows.Forms assembly is not imported by default and the System.Windows...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Mastering PowerShell Scripting - Fourth Edition
Published in: Jun 2021Publisher: PacktISBN-13: 9781800206540

Author (1)

author image
Chris Dent

Chris Dent is an automation specialist with deep expertise in the PowerShell language. Chris is often found answering questions about PowerShell in both the UK and virtual PowerShell user groups. Chris has been developing in PowerShell since 2007 and has released several modules over the years.
Read more about Chris Dent