Scripting Tips and Tricks: Write-Verbose

A couple of weeks ago I reminisced about message boxes. In that post I mentioned that I use the Write-Verbose cmdlet in scripts. Here's how why and how...

Why?

Information sent to the verbose message stream can aid troubleshooting. It's also great for tracking the progress of a script or function.

How?

With version 2 of PowerShell 'advanced functions' made their debut. These are functions that act like cmdlets. To enable this goodness we use the CmdletBinding statement. In doing so we have easy access to a wealth of additional, familiar functionality, e.g. common parameters.

Common parameters are parameters that can be used with any cmdlet. One of these is -Verbose...

Get-WindowsFeature -Name GPMC -Verbose

 

This is how we make use of Write-Verbose in a very simple advanced function:

function Write-VerboseStream {

[CmdletBinding()]

Param ()

Write-Verbose "YAY!"

}

 

Running the Write-VerboseStream function with the -Verbose parameter results in a message to screen.  

 

As stated earlier we need the CmdletBinding statement to access the -Verbose common parameter. We also have to include the Param statement. Notice how both are able to accept additional arguments - ( )

 

Chose words carefully; talk economically.

Write scripts meticulously and embrace verbosity! 

 

More to come on CmdletBinding and Param...