0

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.

YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • 4
    Your question may have an answer [here](https://stackoverflow.com/questions/19754069/powershell-difference-between-write-host-and-write-output) – Walter Mitty Dec 22 '19 at 11:28
  • 4
    Under the hood, it's [Out-Default](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/out-default?view=powershell-6). In addition to the the link above, check out [How PowerShell Formatting and Outputting REALLY works](https://devblogs.microsoft.com/powershell/how-powershell-formatting-and-outputting-really-works/) and [Out-Default: Secrets Revealed](https://get-powershellblog.blogspot.com/2017/04/out-default-secrets-revealed.html) for further reading. – Booga Roo Dec 22 '19 at 12:01
  • 2
    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](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powershell-5.1) – Booga Roo Dec 22 '19 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!). – YorSubs Dec 22 '19 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. – Booga Roo Dec 22 '19 at 13:12

3 Answers3

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"
Vish
  • 466
  • 2
  • 12
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.

Wasif
  • 14,755
  • 3
  • 14
  • 34
1

Write-output.

write-host hi | measure | % count
hi
0

write-output hi | measure | % count
1

'hi' | measure | % count             
1
js2010
  • 23,033
  • 6
  • 64
  • 66