Write-Host Controversy

In the recent post “Rename UPN in AD, AAD and Update Primary SMTP”:

Read the article and based on the blog’s comments, I would be hard pressed to say the majority agreed with Jeffrey Snover. I’m no Powershell guru, not even close and would admit to having some difficulty understanding some of those varied viewpoints.

I wonder if a blanket “Do not use Write-Host” is perhaps a bit draconian. I use Write-Host, quite frequently in fact to the chagrin of Snoverites, as its useful for the following:

  • foregroundcolor
  • nonewline

One particular common usage for me is:

Write-Host "Enter username: " -foregroundcolor Green -nonewline
$Username = Read-Host
# Do stuff with variable $Username

If we have no need to redirect output (which is most of the time for me), perhaps Write-Host doesn’t fall under the blog’s …

Write-Host is almost always the wrong thing to do

… criteria?

Would love to hear what others think. Cheers.

Here is a pretty good excerpt from Jason Helmick, the Program Director for the PowerShell team, and Jeffery Snover, the inventor of PowerShell: PowerShell For Beginners Full Course | PowerShell Beginner tutorial Full Course - YouTube

Looks like the video just reiterates Snover’s blog and the distaste for Write-Host. Helmick and Snover are almost evangelical about this. The video even proposes the use of inbuilt Powershell colorization via Write-Error, Write-Warning etc if we want color.

My point is the zealousness in bagging Write-Host. It may not be suitable for automation but its not undesirable to the point of “almost always”. There’re lots of times where we work in the console and a bit of color never hurts. And why accept the color choices of Write-Error and its ilk when we can have a wealth of color combinations with Write-Host.

Notice the blog is dated 2013 while the video is using Powershell 3.0. Apparently, since Powershell 5.x, Write-Host is controllable by stream output. Not sure if this would change the naysayers perception but just saying Write-Host has its place and can be desirable in the right context.

In the end it is up to you to decide if you want to use it or not. And if you don’t show your code to anyone else you will hardly ever hear any complains. :wink:

We all are grown ups and can decide for ourselfs if we follow the advices from others or have our own contrary oppinions. :+1:t4:

Of course there are cases where you can and should use Write-Host. If you you need to have colored text in your console for whatever reason there’s no way around Write-Host. :man_shrugging:t4:

I love to make my scripts to run silently wihtout user interactions so I don’t need to use it. It always depends on the circumstances.

You’re perfectly right. But it is common sense not to use it for purposses where better options like Write-Debug, Write-Information or Write-Verbose exists. And if you have your reasons and if you are aware of this whole discussion you should just ignore the comments about your use of Write-Host. :wink: :love_you_gesture:t4:

1 Like

I have never claimed to be grown up :slight_smile:

My preference is Write-Output in conjunction with Start-Transcript so I can get everything logged. You can also use

$Host.UI.RawUI.ForegroundColor

to change the color

I subscribe to the categories of scripts discussed in PowerShell In A Month of Lunches: tools, controller scripts. I believe that Write-Host should not be used in tools. The story is different with controller scripts - if there is a business case to use it then use it. If there is no business case to use it then it is best to avoid it.

It’s nothing you have to claim. :wink: You just start acting like and then you are. :stuck_out_tongue_winking_eye: