Prof. Powershell

Fanfare for the Common Parameter Part 1

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.

[Click on image for larger view.] 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:

Parameter Name Parameter Alias Brief Description
Debug db Turn on the debug pipeline
ErrorAction ea Set error action preference at the cmdlet level
ErrorVariable ev Store errors in a user defined variable
OutBuffer ob Control the number of objects in the buffer
OutVariable ov Save pipeline output to a user defined variable
Verbose vb Turn on the verbose pipelin
WarningAction wa Set warning action preference at the cmdlet level
WarningVariable wv 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.

[Click on image for larger view.] 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.

About the Author

Jeffery Hicks is a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 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 works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.

comments powered by Disqus

MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.