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
returnkeyword to return a value from the function. - Call the function defined in step 1 and save the returned value.
- Use
Write-Hostto print the returned value.
function returnOneValue{
$string = "Hello"
return $string
}
$returnedValue = returnOneValue
Write-Host $returnedValue
Hello
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
returnkeyword 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 thereturnkeyword is used. You can learn about classes here.Let’s understand with the help of example:
function returnOneValue{ $string = "Hello" write-output "From Function" return $string } $returnedValue = returnOneValue Write-Host $returnedValueFrom Function HelloAs you can see, even if we used
write-outputin thefunction, it was included in return value from function.In case, you use
classhere, you will get return value which you have actually returned from the function.class test_class { [string]returnOneValue() { $string = "Hello" write-output "From Function" return $string } } $tc = New-Object -TypeName test_class $tc.returnOneValue()Hello
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
returnkeyword to return the values from the function. - Invoke the function defined in step 1 and save the returned value.
- Use
Write-Hostto print the returned value.
function returnMultipleValues{
$firstName = "John"
$lastName = "Williamson"
$age = 50
return $firstName, $lastName, $age
}
$firstName, $lastName, $age = returnMultipleValues
Write-Host $firstName, $lastName, $age
John Williamson 50
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.
function returnMultipleValues{
$firstName = "John"
$lastName = "Williamson"
$age = 50
return $firstName, $lastName, $age
}
$firstName, $age, $lastName = returnMultipleValues
Write-Host $firstName, $lastName, $age
John 50 Williamson
See, $lastName contained the age and $age contained the last name. So, be careful while catching the returned values.
Further reading:
That’s all about how to return value from Function in PowerShell.