Table of Contents
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.
1 2 3 4 5 6 7 8 |
function returnOneValue{ $string = "Hello" return $string } $returnedValue = returnOneValue Write-Host $returnedValue |
1 2 3 |
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
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 thereturn
keyword is used. You can learn about classes here.Let’s understand with the help of example:
123456789 function returnOneValue{$string = "Hello"write-output "From Function"return $string}$returnedValue = returnOneValueWrite-Host $returnedValueAs you can see, even if we used
123 From Function Hellowrite-output
in thefunction
, 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.
1234567891011 class test_class {[string]returnOneValue() {$string = "Hello"write-output "From Function"return $string}}$tc = New-Object -TypeName test_class$tc.returnOneValue()
123 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
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.
1 2 3 4 5 6 7 8 9 10 |
function returnMultipleValues{ $firstName = "John" $lastName = "Williamson" $age = 50 return $firstName, $lastName, $age } $firstName, $lastName, $age = returnMultipleValues Write-Host $firstName, $lastName, $age |
1 2 3 |
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.
1 2 3 4 5 6 7 8 9 10 |
function returnMultipleValues{ $firstName = "John" $lastName = "Williamson" $age = 50 return $firstName, $lastName, $age } $firstName, $age, $lastName = returnMultipleValues Write-Host $firstName, $lastName, $age |
1 2 3 |
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.