write-host vs write-output

Hoping someone can help me out here. I’ve got the following script:

cls $a = "red","green","blue" $b = "purple","green","blue"

write-host “This is how these arrays compare” -foregroundcolor yellow
compare $a $b -includeequal

write-host “”
write-host ‘These are the values that $a has exclusively’ -foregroundcolor yellow

compare $a $b | where {$_.sideindicator -match “<=”}

But the order of output is not what I am expecting. The “write-host” statements are executed first, and then the “compare” statements are output. Run it and you’ll see what I mean.

If I replace the “write-host” statements with “write-output” and eliminate the “-foregroundcolor” parameter (because write-output does not support it) then I get what I want, except that it’s not as easy to read.

Any way I can ensure that code execution follows the order I want, here? I often write scripts that output to the screen, and adding a little color really helps make the output legible sometimes.

There are some deeply important differences between those commands, and those differences lie at the heart of some of the shells most important concepts. It’d be worth picking up “Learn Windows PowerShell in a Month of Lunches” to really work through that.

If the on screen output is literally all you care about, stick with Write-Host exclusively and don’t use anything else. That’s a mental jury rig, though - you definitely want to dig into what’s happening or the shell will continue to get in your face with weird issues ;).

We use the following trick to use color without using Write-host

$ui = (Get-Host).UI.RawUI
$ui.ForegroundColor = "red";
Write-Output "This text will be red"
$ui.ForegroundColor = "white";

Actually, the whole prompt will be red after line 2 and white after line 4.

David, try the following. I corrected the match criteria in you Where statement (it had a space between the less than and the equals). Also, I piped your first compare to Out-Default. (Don, correct my explanation here if I butcher it) The Out-Default is basically causing PowerShell to wait until it has all of the objects from your Compare command before moving on. I had a somewhat similar issue with unexpected output where I was given a good explanation of what’s happening with objects in the pipeline.


$a = "red","green","blue"
$b = "purple","green","blue"
write-host "This is how these arrays compare" -foregroundcolor yellow
compare $a $b -includeequal | Out-Default
write-host ""
write-host 'These are the values that $a has exclusively' -foregroundcolor yellow
compare $a $b | where {$_.sideindicator -match "<="}          

My Output:

This is how these arrays compare

InputObject SideIndicator

green ==
blue ==
purple =>
red <=

These are the values that $a has exclusively

InputObject SideIndicator

red <=

I can’t get the forum to post my output correctly, but you can see for yourself that it works.

Don’t get in the habit of using Write-host. You will regret it later on. Stick with Write-Output and if you need to change colors use the UI.RawUI method. IMO script colors aren’t worth the headache. If I want an interface for a script I’ll use a GUI.

I’ll second what Rick said.

Thanks, Sylvain. That trick prove useful, I’m sure.

Thanks, Kevyn. That “= >” was an odd typo and I’m not sure how it snuck into my copy/paste.

“Out-Default” was what I was looking for. I’ve run into other issues that required I use that, though I don’t recall all the details at the moment (it had to do with a looping query with a “select” statement whose parameters varied based on user input). It was a long time ago and I couldn’t remember the precise solution. However now that you bring it up, “out-default” was the way I dealt with it.

Thanks guys, but I can’t say I agree. I find color to be useful. Here’s a post I wrote on the subject on a blog I keep (readership probably zero, lol!)


Yeah, probably 99% of the time you don’t need color, but that 1% it’s nice to have.

Not a problem. Glad I could help.

Yeah, you could make a little function out of it :

function wh() {

		$ui = (Get-Host).UI.RawUI
		$ui.ForegroundColor = "$color";
		Write-Output "$string"
		$ui.ForegroundColor = "white";