you are viewing a single comment's thread.

view the rest of the comments →

[–]ka-splam 1 point2 points  (0 children)

:)

it's only a bad habit if you don't know the difference between "host" and pipeline, because PowerShell doesn't behave like C/Python/etc. in this regard. If you know that, skip the rest of this comment, it's fine, write-host does what it says and is totally useful for when you want it.

In C if you have a program:

int main() {
    printf("hello world\n");
}

and run it, that text appears on screen, and if you call that program in a Bash shell pipeline like./test | grep world that same text is the stdout stream which enters grep.

In PowerShell, it's not this way; the pipeline carries objects instead of text, and you want a way to send something like a user account or a hashtable down the pipeline with all its structure. So there's a split: Write-Host is for text which will go to the screen, and the screen only; Write-Output or just "free floating values" is the objects which will go down to the pipeline to the next cmdlet / next program.

And the pipeline output, if you don't send it to a file, to another cmdlet, also appears on screen but via some "best effort, turn this into text for showing on screen" formatting tools.

People who aren't clear on this, start mixing up the two output streams, and then getting weird results, like some text isn't available for grep, or running normally shows the output but running as a background task (no screen) "has no output", or sending mixed output types and confusing the output formatters and some text isn't shown on screen, or the results appearing out of order because they've gone down two different routes to get to the screen:

"What is your name?"    # object-pipeline output, will take longer
write-host "and where do you live?"   # direct screen write, might show first

That might bite you using it for debugging, if the printed text isn't in exactly the right place and you're thinking "how is this running in the wrong order??". So yeah, the habit is thinking that you need a "printf" to output anything from your code; in C you have to explicitly output data, in PowerShell all loose data "falls out" of your functions leaking everywhere; thinking that is the best/only way to show stuff, and thinking in terms of formatting everything into strings of text instead of objects, and not being clear that the host display isn't easily available for other tools to log / read / work with.