Good to know: Returnvalues from functions

Many people think that returnvalues from a function in Powershell is a very small or simple thing, but I’ve stumbled a few times over this topic.
In Powershell functions are able to give more than one value back. If you don’t know this, this fact can let you despair.
Lets have a look at a tiny function, which should have on the first look only one returnvalue:

Normally we expect, that this function return a simple “Bye”. Maybe the “World”, although it should immediately written, but not given back as a returnvalue from the function.
That’s wrong.
The Powershell function give us all of this back. Lets prove:

We got “Hello World Bye”. In fact, there are three returnvalues. Actually the variable $rc is an array.

So what is the special thing now? If you have a huge function with many commands and you want a simple returnvalue (like $true, or an integer), this can be a frustrating problem.

If you determine more than one returnvalue and you only want one (but sometimes more than one value is gold), you must pipe the output from the command within the function to null.

Now you get only “World Bye”. It’s possible for every command in the function.

That’s it. Simple but good to know 🙂

Leave a Comment