It is good practice to use the various -Verbose, -Debug and other about_CommonParameters when writing functions because it helps one to write the clean code in a very integrated way with the PowerShell. This blog post focuses on the usage of Verbose command to provide detailed tracking information and status. One of the main benefits about using Write-Verbose command is that you can control, if you need extra detailed information. By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreference variable or using the Verbose common parameter in any command. Also, Write-Verbose writes to the verbose output stream and you can capture it separately.
Using the $VerbosePreference Variable
$VerbosePreference variable determines the Windows PowerShell responds to verbose messages generated by a script, cmdlet or provider, such as the messages
generated by the Write-Verbose cmdlet itself. This has 4 values: Stop, SilentlyContinue, Inquire and Continue. Out of these, SilentlyContinue value allows to supress the verbose output and is the Default value.
To understand the usage of this variable, consider below code snippet:
The output from above code snippet is as below:
As its clear from above output, that if $VerbosePreference variable is set to SilentlyContinue, it will suppress the verbose output and if set to Continue, it will print the verbose output. However, when one uses the -Verbose option directly, it overrides the behavior defined by $VerbosePreference variable.
Using Verbose Output in Functions
To understand how we can control output from a Function, with using verbose parameter, consider below code:
The output from this code is as below:
There is not much to explain here. If we call the function with -Verbose parameter, it will honor the same and prints verbose output from all providers and cmdlet.
Cascading Verbose Output when using Multiple Functions
When you start using -Verbose parameter and you refactor your code, it is often necessary that you need to get Verbose output from all the functions who are being called from one another, so as to form the proper stack trace and output values. Thankfully, in PowerShell, you do not need to do anything special for doing this. PowerShell will first check if the first function was called or not using the -Verbose paramter and then automatically passes it on to the functions being called from inside of it.
Consider below code:
When we call the function Get-SumandDifference with -Verbose parameter, PowerShell notes down the same and passes it to functions Get-Sum and Get-Difference when calling the same. Below output confirms the same:
Summary and Notes
PowerShell makes it easy to generate the Verbose output at times when needed and auto handles the underlying complexities. Using Write-Verbose and other common parameters helps one to write more maintainable code. It is also beneficial for other users, who are using your code, to use verbose output to figure out potential issues.
A copy of the source code used in this blog can be found at here on GitHub under blog/8409 and master branches.
The source code might not be shown, if you are opening this blog post using certain in-app browsers. In such a case, browse the post using proper browser on your device.