Prof. Powershell
Fanfare for the Common Parameter Part 1
- By Jeffery Hicks
- 06/18/2013
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:
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.
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 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.