PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:
- All output is captured, and returned
- The return keyword really just indicates a logical exit point
Thus, the following two script blocks will do effectively the exact same thing:
$a = "Hello, World"
return $a
$a = "Hello, World"
$a
return
The $a variable in the second example is left as output on the pipeline and, as mentioned, all output is returned. In fact, in the second example you could omit the return entirely and you would get the same behavior (the return would be implied as the function naturally completes and exits).
Without more of your function definition I can't say why you are getting a PSMethod object. My guess is that you probably have something a few lines up that is not being captured and is being placed on the output pipeline.
It is also worth noting that you probably don't need those semicolons - unless you are nesting multiple expressions on a single line.
You can read more about the return semantics on the about_Return page on TechNet, or by invoking the help return command from PowerShell itself.
PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:
- All output is captured, and returned
- The return keyword really just indicates a logical exit point
Thus, the following two script blocks will do effectively the exact same thing:
$a = "Hello, World"
return $a
$a = "Hello, World"
$a
return
The $a variable in the second example is left as output on the pipeline and, as mentioned, all output is returned. In fact, in the second example you could omit the return entirely and you would get the same behavior (the return would be implied as the function naturally completes and exits).
Without more of your function definition I can't say why you are getting a PSMethod object. My guess is that you probably have something a few lines up that is not being captured and is being placed on the output pipeline.
It is also worth noting that you probably don't need those semicolons - unless you are nesting multiple expressions on a single line.
You can read more about the return semantics on the about_Return page on TechNet, or by invoking the help return command from PowerShell itself.
This part of PowerShell is probably the most stupid aspect. Any extraneous output generated during a function will pollute the result. Sometimes there isn't any output, and then under some conditions there is some other unplanned output, in addition to your planned return value.
So, I remove the assignment from the original function call, so the output ends up on the screen, and then step through until something I didn't plan for pops out in the debugger window (using the PowerShell ISE).
Even things like reserving variables in outer scopes cause output, like [boolean]$isEnabled which will annoyingly spit a False out unless you make it [boolean]$isEnabled = $false.
Another good one is $someCollection.Add("thing") which spits out the new collection count.
Why not use the return statement?
Explaining Rationale for Function Output Behavior
What are the best practices regarding return values from functions?
Why does my return value print on the screen? How can I avoid that?
Videos
Coming from a managed code background, I use return in just about every function I write. When I want to return a collection to the pipeline, I add a # return comment so I know that I explicitly meant to return the object. For the community and my coworkers... I am on the wrong side of this battle. I'll continually receive code reviews saying I should not use the return statement, but I think thats their choice. I use it for the purpose of being explicit. I've helped the same people who want me to stop using it debug their broken code because they accidentally were returning something else into their pipeline that they didn't want there. Not just once, but at least once a sprint. Each.
So, grand PowerShell community, what's up with the pushback on return?
function test() {
write-Host "Called by: $($MyInvocation.Line)"
}
$a = test
Which outputs
Called by: $a = test
?
I don't think this is possible at all, nor would it make much sense.
It's like expecting the integer 42 to know if it has been stored in the variable $answer.
Once a function returns, only its return value (if any) is stored in a variable (if an assignment is actually used). If Get-Answer returns 42 and you issue the command $answer = Get-Answer, then $answer will indeed contain 42, but no record will be kept of the fact that it was stored there by having it returned from a function; for all intents and purposes, the end results of $answer = 42 and $answer = Get-Answer are identical, if Get-Answer does indeed return 42.
Also, the assignement (if any) only happens after a function returns; the function only provides a return value (if it does); it doesn't and shouldn't care what PowerShell is going to do with this value after it returns; and its return value could very well be discarded instead of being assigned to anything. There is no direct link between $answer and Get-Answer: what PowerShell does when faced with a command like $answer = Get-Answer is:
- Execute
Get-Answer - Grab the return value from
Get-Answer - Store this value in
$answer
None of the players has any knowledge of this link; the function doesnt know what its return value will be used for, and the variable doesn't know where its assigned value comes from.
And even if some record was kept of this assignment having ever happened, it definitely wouldn't have happened yet while Get-Answer was still being executed.