Most PowerShell beginners think that the Return description is the only way for PowerShell functions to output a value. The return statement typically ends the function and hands control back to the block of code that is called it.
But this is not entirely accurate in Windows PowerShell.
In PowerShell, the Return command is applied to stop a function and return control to the user or to return a value from a function.
This post will guide you in creating the Return command to get values back from the PowerShell function return.
What Is Powershell Function Return?
The return keyword ends a script, script block, or function. It can return a value, indicate that the range’s end has indeed been reached, or depart the range at a certain point.
The return keyword can be used by users who are familiar with programming languages like C or C# to explain the reasoning behind leaving a scope.
In PowerShell, even when no declarations contain the Return keyword, the output of each statement is returned. Languages like C and C# return only the value or values the return keyword provides.
Note: Starting with PowerShell 5.0, a language for creating classes was provided. This language uses formal syntax. Nothing else than what you define using the return statement is generated from a method in the context of a PowerShell class.
How To Use Powershell Function Return
Consider the most straightforward PowerShell function:
In a traditional programming language, running this function with parameter 5 (TestReturn(5)) will result in a return value of 10 (integer value).
But using PowerShell, we will note the following output:
As you can see, the PowerShell function that results returns three values. Because of this, the PowerShell Return function is not especially well known.
According to the Microsoft PowerShell reference guide, the return word is out of the current scope, including a script, function, or script block.
In other words, it’s employed to hand responsibility to the parent block when the functional code has finished running. Any additional parameters added to the due to sustained will be back along with the return value to the calling code block.
Please note that the Return command does not need to be specified in the PowerShell function. Any property or object’s value presented immediately in the function’s code will be accessible as the function’s output.
The function’s return type is System. Object, an array, is used. The supplied object illustrations will also be included in the gathering if the function’s output includes any objects (dynamic array).
The array’s length and the items it holds can be displayed:
PowerShell functions, by default, operate in a fashion that, upon execution, writes all values seen while the function is used to the $Result array. Writing PowerShell functions is not always straightforward.
Avoid using echo (an alias for the Write-Output cmdlet) to take the debug information in your PowerShell script to avoid adding more values to a function’s output stream. Use the Write-Host, Write-Verbose, or Write-Debug cmdlets in their place. These cmdlets will not create a function output stream; instead, they will write functions statistics to the console (on-screen):
The Return statement, comparable to the Break command in a PowerShell loop, can be used to hide more data from the turnout sequence. The function is then terminated, and a value is returned using Return.
Return And The Pipeline
PowerShell instantly unscrolls the elements of a selection once you return it from a script block or function, then it passes each item via the route one at a time. This is because PowerShell only processes command once.
The sample function that produces an array of numbers in the next paragraph exemplifies this notion. The Measure-Object cmdlet uses the stored procedure output to determine how many objects are in the pipeline.
Employ one of these two techniques to force a script block or function to restore the array to the pipeline as a single particle:
- Statement for a unary array: You can deliver your return value along the path as a specific object by employing a unary expression, as demonstrated in the example below.
- Write-Output with the NoEnumerate parameter: The Write-Output cmdlet can also be combined with the NoEnumerate option. The return keyword is used in the sample below to use the Measure-Object cmdlet to quantify the objects supplied to the pipelines from the template function.
The Bottom Line
In this article, our TrustGuide team has introduced you to every detail relevant to the Powershell function return. To sum up, PowerShell uses the Return command when a function is meant to finish and return to the caller scope or to provide a value from a function.
Hopefully, this information will serve you well for your needs. Thank you for reading the post. See you around!