Technical Articles
F5 SMEs share good practice.
cancel
Showing results for 
Search instead for 
Did you mean: 
Custom Alert Banner
Joe_Pruitt
Cirrostratus
Cirrostratus

0151T000003d4YXQAY.jpg Welcome to this addition of the PowerShell ABC's where you'll find 26 posts detailing a component of the PowerShell scripting language, one letter at a time.  Today's letter is the letter "I".  For "I", I opted for a couple of operators that you might not know about, but come in very handy with PowerShell's loosely typed framework.  The word for today is "is" (with a double bonus of "isnot" and "as").

In a previous post, I talked about the PowerShell's Arithmetic Operators.  In addition to Arithmetic, there are Assignment (=, +=, -=, *=, /=, %=), Comparison (-[ci]eq, -[ci]ne, -[ci]gt, -[ci]ge, -[ci[lt, -[ci]le, , -[ci]contains, -[ci]notcontains),  Pattern Matching (-[ci]like, -[ci]notlike, -[ci]match, -[ci]notmatch, -[ci]replace), Logical and bitwise (-[b]and, -[b]or, -[b]xor, -[b]not), and Unary (-, +, --, ++) operators as well. 

0151T000003d4YYQAY.jpgBut, you may not have known that there are also operators for working with types. 

The type of an object is key to figuring out what operations can be performed on it.  In PowerShell, variables are loosely typed and can be converted on-the-fly to accommodate certain operators (see my article on Arithmetic Operators for some examples). Sometimes implicit determination as to operations on a variable is not good enough and you would like to explicitly specify them. 

PowerShell provides the following set of operators that let you work with types (table taken from Windows PowerShell in Action by Bruce Payette).


OperatorExampleResultsDescription

-is$true -is [bool]$trueTrue if the type of the left side matches the type of the right side.
$true -is [object]$trueThis is always true - everything is an object except $null.
$true -is [ValueType]$trueThe left side is an instance of a .NET value type.
"hi" -is [ValueType]$falseA string is not a value type, it's a reference type.
"hi" -is [object]$trueBut a string is still an object.
12 -is [int]$true12 is an integer.
12 -is "int"$trueThe right side of the operator can be either a type literal or a string naming the type.
-isnot$true -isnot [string]$trueThe object on the left side is not the same type as the right side.
$true -isnot [object]$trueThe null value is the only thing that isn't an object.
-as"123" -as [int]123Takes the left side and converts it to the type specified on the right side.
123 -as "string""123"Takes the left side into an instance of the type named by the string on the right side.

-is

The -is operator returns true of the object on the left side is of the type specified on the right side.  The object can either be the exact type as that on the right or derived from the type and the type must be specified by a type literal such as [string] or the literal string of the type name "string".  Examples of this are above in the table.

-isnot

The -isnot is the logical negative of the -is operator.  It returns true if the left side is not of, or derived from, the type specified on the right side.  Again, see the examples above.

-as

I haven't mentioned the -as operator, but it is another valuable tool when you want to convert an object at run time to a specific type.  The -as operator is modeled off of the corresponding operator in the C# language with the added features that it can work not only on reference types, but any type you pass it.  Using -as instead of a cast allows you to use a runtime expansion to specify the type, where a cast is specified at parse time.

The following example a list of type literals is looped over and converts the string "0123.45" into those types.  This isn't possible when types are used as operators.

PS> foreach ($t in [int], [string]) { "0123.45" -as $t }
123.45
123
0123.45

One more added benefit to the -as operator versus casting is that when a conversion for a cast fails, an exception is raised while the -as operator will return $null without a runtime error.  This is illustrated with the following example

PS> [int]"a" -eq $null
Cannot convert "a" into "System.Int32". Error: "Input string was not in a correct format."
At line: 1 char: 6
+ [int]" <<<< a" -eq $null
PS> ("a" -as [int]) -eq $null
True

With -is, -isnot, and -as, you should be well on your way to controlling dynamic types.

Version history
Last update:
‎31-Dec-2008 10:46
Updated by:
Contributors