Don’t do that: The following code use multiple Write-Host for verbose messages:
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).
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:
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).