Is there a way to clear the return values inside of a function so I can guarantee that the return value is what I intend? Or maybe turn off this behaviour for the function?<
This is one that caused me some trouble with much larger functions.
One can take the first answer "Pipe output to Out-Null" a bit further. We take the code in our function and embed this in a script block, which we then pipe its output to Out-Null.
Now we don't need to trawl through all the code in the function...like so:
function GetArray()
{
.{
# $AutoOutputCapture = "Off" ?
$myArray = New-Object System.Collections.ArrayList
$myArray.Add("Hello")
$myArray.Add("World")
} | Out-Null
# Clear return value before returning exactly what I want?
return $myArray
}
$x = GetArray
$x
Set variable scope to script or global.
function Get-Greeting {
'Hello Everyone'
$greeting = 'Hello World'
'Ladies and Gentlemen'
$script:greeting = $greeting
}
Get-Greeting
$greeting <== 'Hello World'