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 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.Forms.Form
type does not exist in the current PowerShell session.The -as...