Prof. Powershell

Verbose Pipeline: Tell Me Everything

Get more information than you might have thought was available.

Generally speaking, PowerShell only tells you what you need to know, or at least what it thinks you need to know. But sometimes there might be more to the story, if you know how to ask. This "extra" information can be delivered via the Verbose pipeline. This is a separate pipeline from the one you are used to working in. Understanding how to access this pipeline is an important step in mastering PowerShell, especially when it comes to developing your own advanced functions.

First, all cmdlets share a common set of parameters. One of those parameters is –Verbose. You can try a command like this:

PS C:\> get-service -verbose

But you probably won't see anything. This is because the command must be designed to write verbose output. The only way you can know which ones can is to try. Here's a PowerShell 3.0 command:

PS C:\> get-ciminstance win32_service -filter "startmode='auto' AND state='stopped'" –verbose

As you can see in Figure 1, this cmdlet has been designed to include verbose output.

Invoke-WebRequest cmdlet

Figure 1. (Click image to view larger version.)

It may not be everything you'd hope for but it is better than nothing. Messages written to the verbose pipeline are controlled by the $VerbosePreference variable.

PS C:\> $VerbosePreference
SilentlyContinue

The default behavior of SilentlyContinue in essence turns off the verbose pipeline so anything written to it never comes out. If you think you'd always like verbose output you can set it to Continue.

PS C:\> $VerbosePreference="Continue"

You don't get the formatted object in the text file, you get the complete HTML output. But using the formatted object saved to a variable offers a number of advantages. First, you can use the Status property to verify the Web site:

PS C:\> $blog.StatusCode
200

Now, every command you run will display any verbose output, without having to use –Verbose, like Figure 2.

Invoke-WebRequest cmdlet

Figure 2. (Click image to view larger version.)

If you like this behavior, set the $VerbosePreference variable in your profile script.

When the pipeline is turned on, you can use Write-Verbose to write to it.

PS C:\> Write-Verbose "I am the walrus"
VERBOSE: I am the walrus

But as soon as you turn it off, Write-Verbose runs, but you don't get the output.

PS C:\> $VerbosePreference="SilentlyContinue"
PS C:\> write-Verbose "I am the walrus"
PS C:\>

What this means for your scripting is that you can include as many Write-Verbose lines as you like throughout your script, often displaying variable values or what the script is doing. When the pipeline is turned on you get the messages.

Now, you could simple set $VerbosePreference at the top of your script or function and edit the file everytime you wanted to turn it on or off. But the better approach is to use the cmdletbinding attribute.

Function Get-Foo {
[cmdletbinding()]
Param()

Write-Verbose "Starting Get-Foo"
...

Note that even if your command doesn't take parameters, you need to include the Param() statement. But now I can run Get-Foo without any parameters. The Write-Verbose commands execute but I get no output. When I want the output, all I need to do is include the common –Verbose parameter.

PS C:\> get-foo –verbose

No coding is required other than adding your Write-Verbose commands. You get verbosity simply by adding cmdlet binding.

Don't be afraid to ask for more information and I strongly encourage you to include verbose information in your own scripting projects from the very beginning.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.