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

posted on Friday, January 23, 2009 8:12 AM

PowerShell 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.  For today's letter of "V" I will talk about default statement output, and specifically voidable statements.

void01 In traditional programming languages, statements do not return a value.  Think of the C++ increment operator in the following statement: "a++;".  In this example the value of variable a is incremented by one.  The value of the variable a is not returned from this statement.  In fact, All C/C++/C# statements do not return values.  This is not true for PowerShell though.  In PowerShell, all statements return a value.

This caused a problem for the designers of PowerShell.  If all statements returned values, then this would lead to confusion for traditional programmers out there that were expecting the opposite behavior.  In fact, the increment (++) and decrement (--) operators were almost not included in PowerShell because of this issue.

In the early part of the PowerShell design, developers were confused by the fact that the the following code would produce the listed output.

for($i = 0; $i -lt 10; $i++) { }
1 2 3 4 5 6 7 8 9 10

The reason was that the "$i++" was returning the value of it's operation.  This was clearly not the desired result...

The designers of PowerShell came up with the concept of voidable statements.  This means that certain types of expressions, when used as statements, do not return values.  Voidable statements include assignments and the increment and decrement operators.  When they are used in an expression, they return a value, but when they are used as a standalone statement, they return no value.

If you want to explicitly discard the output of the statement, then you can use the type literal of [void] as a cast for your statement.  Another option is to use the redirection operator to redirect the output to the $null variable.  The following examples illustrate how to force a statement to be voidable.

PS C:\> Write-Output "hi"
hi
PS C:\> [void](Write-Output "hi")
PS C:\> Write-Output "hi" > $null
PS C:\>

Voidable statements are not likely something you'll need to worry about and they won't likely effect the way you use PowerShell as they are designed to behave as you would expect a language to work.



Feedback

10/5/2009 6:31 PM
Gravatar Just stumbled on this series. Nicely done.

Another way to "void" a statement is to pipe it to out-null.

Mike Shepard
10/5/2009 6:59 PM
Gravatar Thanks Mike, it was a great learning experience for me to force myself to pick a term of a specific letter for each post. I guess since I included the [void] cast, I should have included Out-Null as well. Oh well, maybe next round... B-)

BTW, not sure if you caught it or not, but I did a guest spot on Hal and Jon's PowerScripting Podcast Episode #61 where I talked about this series of posts:

powerscripting.wordpress.com/.../episode-61-joe...

-Joe
Joe
1/14/2011 4:19 AM
Gravatar I really admire your design. I do know that is off matter but,did you make this design yourself,or buy from somewhere? Anyway, in my language, there usually are not much good source like this.
C++ Source Code Directory
3/31/2011 5:36 PM
Gravatar
In traditional programming languages, statements do not return a value. Think of the C++ increment operator in the following statement: "a++;". In this example the value of variable a is incremented by one. The value of the variable a is not returned from this statement. In fact, All C/C++/C# statements do not return values.


Is this true? I thought that C/C++ statements all return values, though for readability reasons it's not used much....

Pretty sure that "a++;" returns the value of a prior to the operation, and "++a;" returns the value of the result of the operation.
Ben

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 5 and 6 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