Something that trips me up often and a handy note to others. When calling a PowerShell function and storing the returned value, all output and the return value will be stored.
Some code:
function myFunction() {
Write-Output -InputObject "Hello there"
return "return value"
}
Write-Output -InputObject "Calling function myFunction()"
myFunction
Write-Output -InputObject ""
Write-Output -InputObject "Calling function myFunction(), store result"
$returned = myFunction
Write-Output -InputObject ""
Write-Output -InputObject "Output of `$returned"
Write-Output -InputObject $returnedPS Z:\> ./quirk.ps1
Calling function myFunction()
Hello there
return value
Calling function myFunction(), store result
Output of $returned
Hello there
return value
PS Z:\>
As you can see the value of myFunction() stored in $returned contains both:
- The output of the
Write-Outputcall (Hello there) and... - The actual value returned (
return value).
Easiest way to avoid if using Write-Output calls for debugging in functions, replace with Write-Verbose calls.