Returning More Than One Value

by Aug 6, 2014

All PowerShell versions

If a PowerShell function needs to return more than one value, best practice is to return objects, and store the information in separate object properties.

Here is a simple and charming alternative for some scenarios. Simply return as many pieces of information as you want, and make sure you assign the result to multiple variables:

function Get-MultipleData 
{
  Get-Date
  'Hello'
  1+4
}

$date, $text, $result = Get-MultipleData

"The date is $date"
"The text was $text"
"The result is $result"

The test function emits three pieces of information, and the result is stored in three different variables.

Twitter This Tip! ReTweet this Tip!