Suppressing return values in PowerShell functions

Suppressing return values in PowerShell functions

  • Comments 10

PSMDTAG:FAQ: How do I supress return values in PowerShell functions?

This was asked in a newgroup with the clarification:

What I mean is:
When you call a function Foo that returns a bool, PS will write 'True' or 'False' to the screenby default. Is there anyway to get it to stop writing those return values?

Let's illustrate the issue:

PS> function test {return $true}
PS> test
True
PS>

First let me clarify what is going on here by answering another FAQ:

PSMDTAG:FAQ: What is the difference between Write-Output $x, return $x and $x?

$x and Write-Output $x are exactly the same.  Both of these emit $x to the output stream.  (NOTE: "Write-Output" will show up in RC2 - in RC1 it is called Write-Object.)  By default, the CONSOLE HOST displays anything that goes to the Output Stream on the CONSOLE.  The engine merely makes these objects available to the host, it is the host's decision to do what they will with them (e.g. the Exchange 2007 admin GUI takes these objects and does something completely different with them [they bind them to property sheets and display them]).

Return $x is the same as Write-Output $x; Return .  We support this syntax because it is used so widely in other environments.

PS> function test1
>> {  "test1"
>>    write-Output "test2"
>>    return "test3"
>> }
>>
PS> test1
test1
test2
test3
PS> $x = test1
PS> $x.length
3
PS> $x[0]
test1
PS> $x[1]
test2
PS> $x[2]
test3
PS>

Now back to the original question - can this be supressed?  There are a number of ways to do this but they all entail one or another versions of the following strategy: Don't let the Output stream get to the host.  Here are some examples:

PS> #Refresher - this is what we don't want to happen
PS> test
True
PS> # We can assign the output to a variable
PS> $null = test
PS> # we can cast the expression to void
PS> [void](test)
PS> # We can pipel the objects to Out-Null
PS> test |out-null

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

 

Leave a Comment
  • Please add 4 and 5 and type the answer here:
  • Post
  • Another method to suppress output of return value.
    # You can also rewrite "test | out-null" as "test > $null"

    [^_^]PS[5]>function test { return $true }
    [^_^]PS[6]>test > $null
  • Why do I sometimes get different types?
    Suppressing return values in PowerShell functions
  • Hi!

    Why does Write-Output print an additional empty line when you use two consecutive "`n" in a string? And why doesn't Write-Host do that? See the examples below.

    PS> Write-Output "abc`nABC`nabc"
    abc
    ABC
    abc
    PS> Write-Output "abc`nABC`n`nabc"
    abc
    ABC


    abc
    PS> Write-Host "abc`nABC`nabc"
    abc
    ABC
    abc
    PS> Write-Host "abc`nABC`n`nabc"
    abc
    ABC

    abc
    PS>

    Great blog, by the way. Cheers!
  • Hi C-J,

    This is a known issue.

    Lee
  • Is it possible to suppress just the warning messages that pop on the screen during a command? For instance, if I do the following:

    Get-mailpublicfolder -resultsize unlimited -erroraction silentlycontinue | ft Alias, PrimarySMTPAddress

    I get several hundred warning mixed in with the output I am looking for. These warnings are from exchange 2003 objects that need some tidying up to be fully 2007 compliant (like leading spaces in alias names and such). I would like to be able to output just the filtered results to the screen and not display the warnings. Is this possible?

  • Going back to original post and expanding it a little

    function test {

     write-host "As I process stuffs I may want to display stuffs - like this line"

     return $an_object_or_variable

    }

    how do we suppress writing to console just the return but not the write-hosts, while allowing return to send the object/var  to caller?

  • this worked for me for similar case, but the only returns from a functions were false or true

    function test | Where-Object {$_ -ne $true -and $_ -ne $false}

  • This behaviour totally sucks balls. Powershell needs to change Return to behave as return does in other languages (like C#) not as an output buffer. Powershell shouldn't have had the Return keyword in it's syntax gees what were you thinking MS!

  • Agreed to RobA - this is the reason I am going to abandon PowerSell

Page 1 of 1 (10 items)