0

I can write output from Powershell with the following script:

Write-Host "Hello", "1234"
Write-Output "Hello", "2345"

The output from this program is:

Hello 1234
Hello
2345

So, the Write-Host with a comma separated list puts a space between the items. The Write-Output puts a newline between them. There are lots of differences between the 5 different Write-xxxxxxx statements, but why in the world would the output differ between these 2 statements and how can change Write-Output to act like Write-Host?

WTF Microsoft?

2
  • Write-Host breaks the pipeline so you shouldn't use it to process data. It writes directly to the console. Commented Jun 3, 2015 at 13:06
  • I can recommend the following blog post, which discusses Write-Host and mentions some alternatives: jsnover.com/blog/2013/12/07/write-host-considered-harmful Commented Jun 3, 2015 at 13:43

1 Answer 1

3

Write-Output understands objects and it writes to the pipeline, while Write-Host doesn't since its role is to write to the console host. You generally shouldn't use Write-Host except in special cases because that way you loose object oriented interface of Powerhsell and revert to cmd.exe paradigm.

To get what you want use:

write-output "$('Hello', '2345')"

When you put array inside of the string it is joined and delimited with $OFS separator which is by default <space>.

To understand this more clearly, consider the following example:

> write-output "hello", "world" | % { "item" }
item
item
> write-host "hello", "world" | % { "item" }
hello world
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.