Writing Output from Powershell

One of the things we often want to do when building a PoSh script is to get some output. I first learned about Write-Host, but I learned over time that this isn’t the best way to do this. Most of the early scripts I saw used this, so I was confused until I went through a few of this post: Write-Host Considered Harmful. This is from Jeffrey Snover, the inventor of PoSh.

In the post, he notes that most of the time we don’t want to use Write-Host, because this means we can’t use the output of a command in the pipeline. For debugging you might think that this doesn’t matter, but Write-Debug is better.

He recommend Write-Output, which is something I want to start to use. That, or Write-Verbose, so the user can get more or less output as needed.

Hopefully this will help me to better write PoSh code that others can easily use.

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , . Bookmark the permalink.

2 Responses to Writing Output from Powershell

  1. Greg Moore says:

    Excellent to know. I’ll be right (write?) back. I’ve got more than a few scripts to update!

    Like

  2. way0utwest says:

    Me too! Working on writing about write-debug as well

    Like

Leave a Reply to way0utwest Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.