Clear captured output/return value in a Powershell function
Pipe output to Out-Null
, redirect output to $null
, or prefix the function calls with [void]
:
$myArray.Add("Hello") | Out-Null
or
$myArray.Add("Hello") >$null
or
[void]$myArray.Add("Hello")
Using the information from user1654962, here is the way that I worked around this PowerShell issue. Since, if there is output information, PowerShell will return it as an array, I decided to make sure the function output was always an array. Then the calling line can use [-1] to get the last element of the array and we'll get a consistent return value.
function DoSomething()
{
# Code that could create output
Write-Output "Random output that we don't need to return"
Write-Output "More random output"
# Create the return value. It can be a string, object, etc.
$MyReturnString = "ImportantValue"
# Other code
# Make sure the return value is an array by preceding it with a comma
return ,$MyReturnString
}
$CleanReturnValue = ( DoSomething() )[-1]
I ran into the same issue. In PS a function would return all output pipe information. It gets tricky to | Out-Null
rest of the code in the function.
I worked around this by passing return variable as a reference parameter [ref]
. This works consistently. Check the sample code below.
Note: some of the syntax is important to avoid errors. e.g. parameter has to be passed in brackets ([ref]$result)
function DoSomethingWithFile( [ref]$result, [string]$file )
{
# Other code writing to output
# ....
# Initialize result
if (Test-Path $file){
$result.value = $true
} else {
$result.value = $false
}
}
$result = $null
DoSomethingWithFile ([ref]$result) "C:\test.txt"
I too am having this exact same frustration! I had a function where a variable is supposed to count up $<variablename>
++
Except. I had forgotten the "++" at the end in one iteration. Which was causing the variable's value to be sent to the output buffer. It was very aggravating and time consuming to find! MS should have provided, at minimum, an ability to flush the output buffer before the user gets to assign their desired return value. Anywhew... I believe I have a work around.
Since I assign a return value at the end of the function, my desired value will always be the last entry of the output buffer array. So I've changed the way I call my functions from:
If (FunctionName (parameters)) {}
To:
If ((FunctionName (parameters))[-1]) {}
My particular scenario bit me when I was trying to capture a function $False return. because of my typo above, my IF statement saw it as true because of the garbage in the output, even though my last function output was "Return $False". The last output assigned from the function was the $False value due to the problem resulting from the typo causing mis-calculation. So changing my Function Return Evaluation to:
If (!((FunctionName (parameters))[-1])) {#something went wrong!}
Did the trick by allowing me to evaluate only the last element of the output array. I hope this helps others.