Fanfare for the Common Parameter Part 1
- By Jeffery Hicks
When you look at help for a PowerShell cmdlet you probably noticed something labeled CommonParameters. If you don't know what I'm talking about see Figure 1.
But you probably just ignored it (which is perfectly understandable). If you create an advanced PowerShell function or script you can also get these mysterious common parameters by using [cmdletbinding()]. What is this all about?
Without getting too far into dev-speak, every cmdlet has a set of common parameters that are defined automatically when a cmdlet is developed. In a script, PowerShell will also add them when it sees the cmdletbinding attribute. These attributes are listed in the following table:
||Turn on the debug pipeline
||Set error action preference at the cmdlet level
||Store errors in a user defined variable
||Control the number of objects in the buffer
||Save pipeline output to a user defined variable
||Turn on the verbose pipelin
||Set warning action preference at the cmdlet level
||Store warnings in a user defined variable
Over the next few lessons we'll look at some of these in more detail. (If you can't wait, you can get a head start by reading the About_CommonParameters help file.) One common parameter I encourage you to try out is –Verbose.
When you run a cmdlet with –Verbose, any messages written to the verbose pipeline will be displayed. Using –Verbose has the effect of dynamically setting $VerbosePreference to Continue. But the cmdlet has to be written to include these messages. The only way you'll know is by trying as you can see in Figure 2.
Get-Service doesn't have any verbose processing so even though I used the parameter nothing happened. But Get-CimInstance was written to put out verbose messages so I get something. If you write an advanced script or function and use cmdletbinding, then you can include lines like this in your script.
Write-Verbose "Starting Get-Miracle"
Write-Verbose "Connecting to magic beanstalk"
When you run the script or function with –Verbose you'll see the messages. Otherwise, you won't. Next time we'll continue our exploration of common parameters.
Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide.
You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)