Don’t do that #9: Use Write-Host for verbose messages

By | March 20, 2015

Don’t do that: The following code use multiple Write-Host for verbose messages:

Do that: Write-Host should not be used for verboese message, Write-Verbose exists for this purpose.

It’s better to use Write-Verbose because you can just turn on/off without the need to remove these lines “Write-Verbose”.
In case you decide to use Write-Host for verbose messages, you need to remove these lines (or comment them).

You can use -Verbose common parameter to turn on verbose only for a specific cmdlet, an example here with a function:


As you can see, we can display the verbose messages when we want by using the -Verbose parameter (switch).
The inconvenient with Write-Host is that we can’t just turn on/off that way.

It is recommended to use the -Verbose parameter instead of modifying the preference variable $VerbosePreference (it pollutes the user’s session).


One thought on “Don’t do that #9: Use Write-Host for verbose messages

  1. Pingback: Don't do that #8 : Get all properties instead of specific one - Powershell Guru

Leave a Reply

Your email address will not be published. Required fields are marked *