Search
Joe Pruitt - A Software Architect's take on Network Security
You are here: DevCentral > Weblogs

posted on Wednesday, December 31, 2008 11:46 AM

PowerShell_2 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. 

TypingMonkeyLargeBut, 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).


Operator Example Results Description

-is $true -is [bool] $true True if the type of the left side matches the type of the right side.
$true -is [object] $true This is always true - everything is an object except $null.
$true -is [ValueType] $true The left side is an instance of a .NET value type.
"hi" -is [ValueType] $false A string is not a value type, it's a reference type.
"hi" -is [object] $true But a string is still an object.
12 -is [int] $true 12 is an integer.
12 -is "int" $true The right side of the operator can be either a type literal or a string naming the type.
-isnot $true -isnot [string] $true The object on the left side is not the same type as the right side.
$true -isnot [object] $true The null value is the only thing that isn't an object.
-as "123" -as [int] 123 Takes 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.



Feedback

1/17/2009 6:03 PM
Gravatar $true -isnot [object], False ?
Andy
7/19/2010 8:29 PM
Gravatar T h a n k y o u !
go to uggs outlet
uggs sale online

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 6 and 1 and type the answer here:

Blog Stats

Posts:379
Comments:1067
Stories:1
Trackbacks:301
  

Article Categories

  iRules
  

Image Galleries

  

Joe's bookshelf: read

The Lost Gate
4 of 5 stars
This one started slow but I got really got into it about 1/3 of the way through. If you are an Ender's Game fan, you'll probably like this one as well.

goodreads.com


82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or