Return Value from Function in PowerShell

Return value from function in PowerShell

Using the return Keyword

PowerShell allows us to use the return keyword to return single or multiple values from a function. Let’s learn both of the scenarios.

Use the return Keyword to Return a Single Value

To return a single value from a particular function:

  • Create a function that returns something, a string, a number or anything else.
  • Inside the function defined in the previous step, use the return keyword to return a value from the function.
  • Call the function defined in step 1 and save the returned value.
  • Use Write-Host to print the returned value.

In the above script, we created a function named returnOneValue; you can name it whatever you want. Inside this function, we created and initialized a variable named $string with a string type value, Hello. Next, we used the return keyword to return the value of the $string variable.

Outside this function block, we called it and saved the returned value in the $returnedValue variable. Finally, we used Write-Host cmdlet to print the value of $returnedValue on the PowerShell Console.

The return keyword exits a script, function, or script block. We can also use it to exit a scope at a particular point, return the specified value, or denote that the end of the scope has been reached.

In PowerShell, each statement’s output is returned even when the return keyword is not used. As of PowerShell 5.0 and above, we can create classes by using formal syntax. In classes, nothing will be returned unless the return keyword is used. You can learn about classes here.

Let’s understand with the help of example:

As you can see, even if we used write-output in the function, it was included in return value from function.

In case, you use class here, you will get return value which you have actually returned from the function.

Use the return Keyword to Return Multiple Values

To return multiple values from the specified function:

  • Create a function that returns multiple values, strings, numbers or anything else.
  • Inside the function defined in the previous step, use the return keyword to return the values from the function.
  • Invoke the function defined in step 1 and save the returned value.
  • Use Write-Host to print the returned value.

This script is similar to the previous example, but we returned multiple values separated by a comma. Similarly, we used multiple variables ($firstName, $lastName, $age) to store the returned values from the returnMultipleValues function. Note that we used $firstName, $lastName, and $age variables inside and outside the function, and both are different. Additionally, the order must be the same as the return statement has; otherwise, we will have incorrect values. See the following example to understand.

See, $lastName contained the age and $age contained the last name. So, be careful while catching the returned values.

That’s all about how to return value from Function in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *