How To Tell If The Verbose Parameter Of A Function Is From [CmdletBinding()] Or Manually Added

Pardon the long title. I had a task recently to go through a big folder full of scripts written by random people with equally random skill levels. Lots of the scripts had a -Verbose parameter, but they weren’t all done correctly.

Some scripts correctly included the [CmdletBinding()] line above the param() block. Some just had a [Switch]$Verbose parameter (wrong). Others had both (double wrong, script won’t even run).

Consider the following three functions, which illustrate the three categories I was dealing with.

function do-verbose1 { param([switch]$Verbose) @{} + $psboundparameters }                  
function do-verbose2 { [cmdletbinding()] param() @{} + $psboundparameters }                
function do-verbose3 { [cmdletbinding()] param([switch]$Verbose) @{} + $psboundparameters }

The first one is bad, the second one is good, the third one is double bad.

Here’s a quick way you can check the scripts using PowerShell so you don’t have to open them all up.

2016-12-06-09_41_54-powershell

As you can see, the first one has a parameter of -Verbose so Get-Help will show you info about it. The second one returns nothing. The third one returns an error.

For checking real scripts that have other parameters, you should pipe the results into where-object { $_.Name -eq ‘Verbose’ } to eliminate other parameters from being returned.

Written on March 1, 2017