write-host vs write-output

This topic contains 12 replies, has 6 voices, and was last updated by  Snak3d0c 4 weeks ago.

  • Author
    Posts
  • #80083

    David Flores
    Participant

    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

    Don Jones
    Keymaster

    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

    Sylvain Martel
    Participant

    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

      Snak3d0c
      Participant

      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

    Kevyn
    Participant

    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

      Kevyn
      Participant

      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

      Kevyn
      Participant

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

    • #80176

      David Flores
      Participant

      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

      Kevyn
      Participant

      Not a problem. Glad I could help.

    • #80173

      David Flores
      Participant

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

  • #80146

    Rick
    Participant

    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

    Kevyn
    Participant

    I'll second what Rick said.

You must be logged in to reply to this topic.