Doing More with PowerShell Verbose Messages

Posted on February 24, 2017 by Jeff Hicks in PowerShell with

Whenever I teach about PowerShell scripting I always stress the value of using verbose messages in your functions and scripts. Assuming you are using cmdletbinding, and why wouldn’t you, you can insert Write-Verbose statements throughout your script. These statements won’t do anything unless your command is run with the common -Verbose parameter. However, these statements can be very useful for tracing and troubleshooting. I use these Verbose statements to help me track what my command is doing so that if it fails I have a better idea of where it failed and what it was doing. I don’t enjoy debugging and dealing with the interactive debugger and having Write-Verbose statements makes my life easier. Here are some ideas on how you might want to start using Write-Verbose.

 

 

First, if you want to try some of these things right from the command prompt, turn on your verbose pipeline.

When you are done testing, set it back to the default.

Now you can run Write-Verbose commands to see what they look like. Remember that anything you want to write with Write-Verbose must be a string or at least be able to be treated as a string.

You may need to use subexpressions as well. One suggestion is to include a timestamp in your verbose message.

inserting a timestamp

Inserting a timestamp (Image credit: Jeff Hicks)



A timestamp alternative

A timestamp alternative (Image credit: Jeff Hicks)

One thing that I tend to do in all my functions is use a verbose message to indicate what scriptblock is being processed.

The Begin and End blocks include code to display the command name. This is very handy if you have a function that is calling another function. You may also be wondering about the spacing of END and BEGIN. I did that so that the verbose output would align nicely and be easier to read.

verbose scriptblocks

Verbose scriptblocks (Image credit: Jeff Hicks)

What about combining some of these things?

detailed scripting messages

Detailed scripting messages (Image credit: Jeff Hicks)

How about a variation that shows how much time is elapsing? Here’s a variation that displays a timespan calculated from the first command.

Verbose output with a timespan

Verbose output with a timespan (Image credit: Jeff Hicks)

One of the reasons I like to use Write-Verbose is that if someone is running one of my commands and encounters an error, I can have them start a transcript, which now works in the PowerShell ISE, run the command with -Verbose, stop the transcript, and send it to me. I can then see exactly what is happening (or not) for the user. With that in mind, I thought it might be helpful to include some verbose metadata.

That might be a bit much to include in every script. What could be easier is an external function I could load in my profile.

I could then call this code in my function.

verbose metadata in action

Verbose metadata in action (Image credit: Jeff Hicks)

There’s really no limit to what you could include in a verbose message. The only guidance I would offer is to make it meaningful. I also encourage you to make your verbose output easy to read.

Sponsored

Are you intrigued? Here are some other elements you might consider adding to your verbose messaging:

  • Username
  • Computer name
  • Parameter set name
  • PSBoundparameters
  • PowerShell version

I hope you’ll let me know if this was helpful and how you start using it.

Tagged with , ,

Register for this upcoming webinar on Petri.com
Webinar: Accelerate Smart Factory ROI with Deloitte and HPE’s Digital IoT

Join HPE and Deloitte for a discussion on how to take advantage of IT and OT convergence to deliver the Factory of the Future

Tuesday, October 24, 2017
at 2 p.m. EST

Register for this upcoming webinar on Petri.com
Webinar: Accelerate Smart Factory ROI with Deloitte and HPE’s Digital IoT

Join HPE and Deloitte for a discussion on how to take advantage of IT and OT convergence to deliver the Factory of the Future

Tuesday, October 24, 2017
at 2 p.m. EST