O'Reilly logo

Pro Windows PowerShell by Hristo Deshev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

4.2. Passing Parameters and Returning Values

None of the blocks that you have seen so far executed an action that interacted with the outside world in any sophisticated way. Now that I've mentioned expressions, we can look at blocks from the perspective of an operation that can be used in an expression. Operations take parameters and return values after they are evaluated. Let's start with return values.

Returning a value from a script block simply requires that you output an object that will not be consumed by a cmdlet or another expression. Here is a block that returns a number:

PS> $number = { 5 }
PS> &$number
5
PS> 1 + (&$number)
6

As you can see from the last command in the previous example, the returned number can be used in expressions ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required