Search notes:

PowerShell: Write-Host vs 'string'

In PowerShell, using write-host $obj writes a textual representation of the object $obj directly to the host's console without putting it into the pipeline.
#
#  Pipe an array of strings ...
#
  'foo', 'bar', 'baz'   |
#
#  ... into foreach-object
#
   foreach-object {
#
#   ... which checks if the
#       string is bar ...
#
   if ($_ -eq 'bar') {
#
#       If it is bar, the object is written
#
        write-host $_
   }
   else {
#
#  ... otherwise, it is passed on in
#      the pipeline:
#
       $_
#
   }
}
Github repository about-PowerShell, path: /cmdlets/host/write/vs-string.ps1
when this script is invoked, it prints
foo
bar
baz
Seemingly, there is no differences between writing a string using write-host and simply state a string (which passes it on to the next command in the pipeline.
However, if the output of this script is piped into a command, it becomes clear that the string "bar" has «disappeared» from the pipeline:
./vs-string.ps1 | foreach-object {
   "I have received the object $_"
}
Github repository about-PowerShell, path: /cmdlets/host/write/vs-string-use.ps1
Now, the output is:
I have received the object foo
bar
I have received the object baz

Index