write-host vs write-output

Welcome Forums General PowerShell Q&A write-host vs write-output

This topic contains 12 replies, has 6 voices, and was last updated by

 
Participant
1 year, 2 months ago.

  • Author
    Posts
  • #80083

    Participant
    Points: 0
    Rank: Member

    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.

  • #80087

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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 ;).

  • #80092
    Syl

    Participant
    Points: 30
    Rank: Member

    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.

    • #80200

      Participant
      Points: 0
      Rank: Member

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

      	
      function wh() {
      	   param(
      			[Parameter(Mandatory=$True,Position=0)]
      			$color,
      			[Parameter(Mandatory=$true,Position=1)]
      			$string
      		)
      
      		$ui = (Get-Host).UI.RawUI
      		$ui.ForegroundColor = "$color";
      		Write-Output "$string"
      		$ui.ForegroundColor = "white";
      	}
      
  • #80129

    Participant
    Points: 1
    Rank: Member

    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.

    https://powershell.org/forums/topic/unexpected-line-space-in-verbose-output/

    $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 "< ="}          
    
    • #80131

      Participant
      Points: 1
      Rank: Member

      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 <=

    • #80137

      Participant
      Points: 1
      Rank: Member

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

    • #80176

      Participant
      Points: 0
      Rank: Member

      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.

    • #80180

      Participant
      Points: 1
      Rank: Member

      Not a problem. Glad I could help.

    • #80173

      Participant
      Points: 0
      Rank: Member

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

  • #80146

    Participant
    Points: 0
    Rank: Member

    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.

  • #80147

    Participant
    Points: 1
    Rank: Member

    I'll second what Rick said.

    • #80179

      Participant
      Points: 0
      Rank: Member

      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!)

      Spruce Up Your Scripts With Menus and Color: Part III

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

The topic ‘write-host vs write-output’ is closed to new replies.