1

When I type "my text" on a line by itself, the text is output to the console, but is that using Write-Host or Write-Output by default (and how can I see that - I mean, I tried using | Get-Member to get a clue but was not sure)?

The reason that I'm asking is that I'm aware of the known issues with Write-Host breaking sequential output in a script that has pipeline commands nearby.

5
  • 4
    Your question may have an answer here Commented Dec 22, 2019 at 11:28
  • 5
    Under the hood, it's Out-Default. In addition to the the link above, check out How PowerShell Formatting and Outputting REALLY works and Out-Default: Secrets Revealed for further reading. Commented Dec 22, 2019 at 12:01
  • 3
    Also, notable is the difference in behavior between versions: "Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information stream. This enables the capture or suppression of data written using Write-Host while preserving backwards compatibility." From the help for Write-Host Commented Dec 22, 2019 at 12:05
  • 2
    Excellent, thanks. Do you know why Microsoft chose to deliberately cripple Write-Information? Write-Host is a wrapper for Write-Information which means that the -ForegroundColor and -BackgroundColor functionality must be inside Write-Information, but Microsoft have deliberately broken and crippled that functionality in Write-Information, but allow it in Write-Host. Sure, they maybe did it as previous Write-Information never had these switches, but it just blows my mind that they added this functionality to 5.0 Write-Information but then cripple it (even though it's a non-breaking change!). Commented Dec 22, 2019 at 13:00
  • 2
    Sorry, wish I knew, but give it some time. Someone may come along with a pointer to that information. Also, don't be surprised if this gets marked as a duplicate for that first link. It helps others find their way to it in case the comments are deleted, which frequently happens. Commented Dec 22, 2019 at 13:12

3 Answers 3

2

So, I did a small test to figure this one out. Out-Host and Write-Host, cannot be redirected to any stream whatsoever. This is how they work, they simply write to whatever is the default calling stream.

Using this logic,

"string1" | Out-host *>$null

will give the output as "string1", since it cannot be re-directed. But,

"string1" *>$null

will display no output, hence proving, writing any text is the same as passing it to the Write-Output cmdlet:

# All are same
"string"
"string" | Write-Output
Write-Output "string"
Sign up to request clarification or add additional context in comments.

Comments

1

There are several methods of displaying text in powershell. The write-host displays the text itself in the console. It is host dependent. Next, write-output writes output from a pipeline, so the next command can receive it. It's not necessary it will display text every time. [Console]::WriteLine is quite same like write-host. And Out-Default sends the output to the default formatter and to the default output cmdlet. It is automatically added by powershell itself at the end of every pipeline. If the input object is not string it decides what to do. So, it is Out-Default which does this.

Comments

1

Write-output.

write-host hi | measure | % count
hi
0

write-output hi | measure | % count
1

'hi' | measure | % count             
1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.