Return behavior in PowerShell
suggest changeIntroduction
return exits the current scope, which can be a function, script, or script block. In PowerShell, the result of each statement is also output, even without an explicit return, and even when return is only marking the end of the scope.
Remarks
You can read more about return semantics on the about_Return page, or by running Get-Help about_Return.
Notable Q&A question(s) with more examples/explanation:
- http://stackoverflow.com/questions/10286164/function-return-value-in-powershell
- http://stackoverflow.com/questions/22663848/powershell-function-doesnt-have-proper-return-value
about_return on MSDN explains it succinctly:
The Return keyword exits a function, script, or script block. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.
Users who are familiar with languages like C or C# might want to use the Return keyword to make the logic of leaving a scope explicit.
In Windows PowerShell, the results of each statement are returned as output, even without a statement that contains the Return keyword. Languages like C or C# return only the value or values that are specified by the Return keyword.