Return Multiple Values from PowerShell Function

In PowerShell functions, by default, you can return only one object at a time. Since PowerShell does not apply any restriction on data type returned, it created a lot of possibilities on what can be returned as an output of the function. So if one needs to return multiple values or objects, it is generally suggested to create an array of the objects and then return the array. If the underlying values are simple strings, some would create a custom PSObject and then return the PSObject. In this blog post, we will discuss the other methods to return the multiple values from PowerShell functions.

The Simple Return Method

Below is one of the simple return methods, in which we return the Sum of the two integers by calling Get-Sum function. Since it needs only one object to return, we do not need to perform any special operations:

Below is the output from one of the runs:

returning single value from PS function

The same can be extended to return an array of objects or a custom PowerShell Object. Again, since we are returning single object, at receiving end we do not need to do anything else.

Returning Multiple Arrays using Return Keyword

Let’s say that we need to return multiple arrays from PowerShell function. In below PowerShell function, we have created an array which contains names of the Persons as $PersonName and another array which contains ages of the Persons as $PersonAge:

We can return those arrays by passing them using return keyword and separating using a comma. Again at the receiving time, we need to refer them consecutively.

Below’s the output from the code run:

return multiple arrays using return keyword

Returning Multiple Arrays without using Return Keyword

We can also return multiple Objects without using Return Keyword in a less conventional way. In below PowerShell function, we have created an array which contains names of the Persons as $PersonName and another array which contains ages of the Persons as $PersonAge:

When a PowerShell function outputs value, it creates an array behind the scenes and returns that array. So at the receiving time, we need to grab hold of items in the same order, as they would have been passed to array. Since we are returning arrays, it creates a little complexity at the receiving end.

Below’s the output from the code run:

return multiple arrays without using return keyword

Return multiple values using Hashtable

Here comes the scary one :). We can also use hashtables to create symbolic arrays and then return the hashtable using return keyword. Below code is the one of the examples for same:

We can then use iterate through hashtable using keys at the receiving end. Below is the output from code run:

return multiple values using hashtables

Summary and Notes

We discussed several ways for returning multiple objects from PowerShell function. However, you need to account for the same at the receiving end to make sure it works as intended.

A full copy of the source code can be found from this GitHub repository under the blog/8384 and master branches.

You may not be able to see source code posted if you are using certain in-app browsers. In such a case, copy the blog link and open it in full or mobile versions of browsers directly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s