Don’t do that: The following code use multiple Write-Host for verbose messages:
1 2 3 |
Write-Host -Object "The value of the variable7 is $var7" Write-Host -Object 'Entering in the 2nd If condition' Write-Host -Object "The value of the variable i is : $i" |
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).
1 2 3 |
Write-Verbose -Message "The value of the variable7 is $var7" Write-Verbose -Message 'Entering in the 2nd If condition' Write-Verbose -Message "The value of the variable i is : $i" |
You can use -Verbose common parameter to turn on verbose only for a specific cmdlet, an example here with a function:
1 2 3 4 5 6 7 8 9 10 11 12 |
function Get-Something { [CmdletBinding()] Param ( [string[]]$Path ) Write-Verbose -Message 'Step 1' Write-Host -Object 'Step 1' } |
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).