Consider the following PowerShell script:
function Alpha {
# write-output 'Uncomment this line and see what happens.';
return 65;
}
function Bravo {
$x = Alpha;
$y = $x -eq 65;
return $y;
}
$z = Bravo;
echo $z;
On my computer (which is running Windows XP SP3, .NET 4.0, and PowerShell 2.0 RTM), when the script is run, the script’s output is as expected (True). However, when “the line” is uncommented (and the script is run again), instead of seeing the same output prepended by Uncomment this line..., I am only seeing 65. Can someone please explain what is going on? Thanks.
Write-Outputjust writes an object to the pipeline. If you need a message displayed on the screen, useWrite-Host.So, to take apart what happens here, that was kind of a preliminary. If you uncomment the line, both the string
'Uncomment this line and see what happens.'and the number65are outputs of the function, so when callingBravothe variable$yno longer holds just a single value but instead the array'Uncomment this line and see what happens.',65.Now the comparison operators work differently if the left operand is an array instead of a scalar value. If the left operand is an array they simply return all elements from the array where the condition would be
$true. In this case, since you compare with65, it will return all items that are equal to65. So the result is no longer a boolean but instead an array of objects (or in this case, just a single object) –65.