Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Write-Output inside "if" does not output anything

Tags:

powershell

I am learning PowerShell recently, and found a behavior that I cannot understand. Let me explain in code below:

function func1()
{
    Write-Output "output from func1()"
    func2
    return $true
}

function func2()
{
    Write-Output "output from func2()"
    func3
}

function func3()
{
    Write-Output "output from func3()"
}

Write-Output "*** 1. run alone ****"
func1

Write-Output "*** 2. run inside if ****"
if (func1) {
    #do nothing
}

It is strange when func1 is called directly, it can output message as expected, but if put inside "if" statement, it will not. See output as below:

*** 1. run alone ****
output from func1()
output from func2()
output from func3()
True
*** 2. run inside if ****

You can see it's empty. Why is that, and how can I enable the output like the first example? Thanks!

like image 749
bobyuan Avatar asked Jul 02 '14 07:07

bobyuan


People also ask

How do you Write-output in PowerShell?

Write-Output sends objects to the primary pipeline, also known as the "output stream" or the "success pipeline." To send error objects to the error pipeline, use Write-Error . This cmdlet is typically used in scripts to display strings and other objects on the console.

What is the difference between Write-host and Write-output in PowerShell?

In a nutshell, Write-Host writes to the console itself. Think of it as a MsgBox in VBScript. Write-Output , on the other hand, writes to the pipeline, so the next command can accept it as its input. You are not required to use Write-Output in order to write objects, as Write-Output is implicitly called for you.

How do I print a message in PowerShell?

The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.

What is out null in PowerShell?

The Out-Null cmdlet sends its output to NULL, in effect, removing it from the pipeline and preventing the output to be displayed at the screen.


2 Answers

PowerShell functions are not like their traditional language counterparts: they can output multiple things. When you're using Write-Output, you are sending things down the pipeline, so func1() will return both a String and a Boolean. If you try this:

$return_values = func1;
$return_values | Get-Member

You will see that you get both System.String and System.Boolean objects in $return_values. Indeed the 'return' statement is more like an 'exit' or 'break' in traditional languages: it marks an exit point, but the function will actually return with whatever was already outputted (+ the optional return argument).

The quick fix to the issue is to change Write-Output to Write-Host, though they do slightly different things.

like image 144
PeterK Avatar answered Nov 16 '22 02:11

PeterK


You can achieve desired results by replacing Write-Output with Write-Host. This is suitable only if your main concern is to produce output in the console as Write-Host does not go to stdout stream.

You can read more about difference between the 2 cmdlets in this SO thread :Which should I use: "Write-Host", "Write-Output", or "[console]::WriteLine"?

like image 37
Raf Avatar answered Nov 16 '22 02:11

Raf