informationaction

A Powershell alias for outputting info from functions

In most common programming languages, a function (or method) defines a block of code that does some work internally, and returns a value at the end, either implicitly (the result of processing the last line is returned), or explicitly (a keyword such as return is used to specify the return-value). In Powershell on the other hand, everything is returned out. Take the following example for instance:

function get-an-array {
    "Creating an array..."
    $arr = "one","two","three"
    "Array created; returning it now:"
    return $arr 
}

#calling the function:
$myArray = get-an-array

In most languages, you would expect $myArray to contain just that – an array after executing the code above. Also, you might have expected the messages in between to have been written out to the screen. Not so! The actual content of the resulting variable will be as follows:

Continue reading