How to Return Multiple Values from Function
Problem
Typically we can only return one value or object from a function. However, PowerShell allows us to return multiple values. We have a choice whether we want to literally return multiple values or group them into an object or a data structure. The latter will make the calling code simpler and more readable.
In this article, we will show you how to return multiple values from PowerShell function.
Solution
To return multiple values, we have options whether we want to literally return multiple values or wrap the values into an object or a data structure like Array, HashTable or Tuple. The latter is preferred when we want to pass around the values.
Let’s take a look at some examples.
Specifying All the Values to Return
This is the most straightforward way to return multiple values. We specify all the values along with return
keyword, then in the calling code we destructure all the values before using them accordingly.
Function Test()
{
$FirstName = "John"
$LastName = "Doe"
return $FirstName, $LastName
}
$firstName, $lastName = Test
$firstName
$lastName
Below is the output if we execute above script.
Return Values Using Array Explicitly
We can wrap the values into an Array and return it explicitly.
Function Test() {
$fullName = @()
$fullName += "John"
$fullName += "Arne"
$fullName += "Riise"
return $fullName
}
$fullName = Test
$fullName[0]
$fullName[1]
$fullName[2]
The other way to return Array explicitly is as follows:
Function Test() {
$firstName = "John"
$middle = "Arne"
$lastName = "Riise"
return @($firstName, $middle, $lastName)
}
$fullName = Test
$fullName[0]
$fullName[1]
$fullName[2]
Or
Function Test() {
$fullName = @()
$fullName = "John", "Arne", "Riise"
return $fullName
}
$fullName = Test
$fullName[0]
$fullName[1]
$fullName[2]
Return Values Using Array Implicitly
By default, simple return value(s) can be treated as an array. Not only the ones explicitly to be returned, but also console output is a return value, thus it becomes implicit array.
function Test() {
Write-Output "John"
"Arne"
return "Riise"
}
$fullName = Test
$fullName[0]
$fullName[1]
$fullName[2]
Return Values Using HashTable
We can also wrap the values into a HashTable by specifying key-value pairs. In this case, the key is the property we are going to use. Then, we return the HashTable.
Function Test() {
$FullName = @{
FirstName = "John"
LastName = "Doe"
}
return $FullName
}
$fullName = Test
foreach ($key in $fullName.Keys) {
Write-Host "$key is $($fullName[$key])"
}
Below is the output if we execute above script.
Return Values Using PSCustomObject
We can also return values using PSCustomObject
. This is similar with using HashTable, but we need to add PSCustomObject
keyword before @
symbol. Other than that, we specify the object property and its value. In order to get the value, the caller code only needs to specify object property and use it accordingly.
Function Test() {
$FullName = [PSCustomObject]@{
FirstName = "John"
LastName = "Doe"
}
return $FullName
}
$fullName = Test
Write-Host "FirstName is $($fullName.FirstName)"
Write-Host "LastName is $($fullName.LastName)"
Below is the output if we execute above script.
Return Values Using Tuple
Tuple is a lightweight data structure that can group some elements. However, it has downside compared to object. We cannot use strongly typed name, thus we access the element using item number or index like an array.
Function Test() {
return [Tuple]::Create("John", "Arne", "Riise")
}
$fullName = Test
$fullName.Item1 # $fullName[0]
$fullName.Item2 # $fullName[1]
$fullName.Item3 # $fullName[2]
Conclusion
There are many ways to return multiple values from a function: specifying all the values, using explicit or implicit array, HashTable, PSCustomObject or Tuple. I hope this article helps you to learn how to return multiple values from a function in PowerShell.