PowerShell V2 & Cmdlet Keyword
In the latest entry in the official PowerShell Blog, Jeffrey Snover leaves a little tidbit about a change coming on the next CTP of PowerShell V2:
The
cmdletkeyword is going away and we'll just havefunction. Notice that now you can specify the[Parameter()]attribute on parameters. When you do that, we treat the function like a cmdlet.
I don’t mind getting rid of the cmdlet keyword; honestly, but is the new syntax that much of an improvement? So now instead of an explicit keyword telling you whether something is a function of a cmdlet, you have an attribute spilled around the function parameters? Not sure if that’s good or not, but sounds confusing to me, at first glance.
Maybe what rubs me the wrong way, however, is the [Parameter] attribute. I’m OK with using an attribute to specify the metadata associated with the parameters, honestly. But the whole Param + [Parameter] thing just reads redundant and ugly:
function Emit-XML {
Param ([Parameter(Mandatory=$true,ValueFromPipeline=$true)]$object)
...
} Can anyone more familiar with this chime in? Am I the only one slightly bothered by this?





Thomas,
For a number of basic functions, this notation will not be necessary, but what it does allow is greater flexibility in working with pipelined objects and saving you from writing some argument parsing.
In the first case, by allowing a value from the pipeline, you are allowing the PowerShell parser to take a look at objects coming in to the function and fill out the parameters based what properties are available.
In the second scenario, by setting the parameter attribute, you can set up groups of parameters, set up optional parameters, and not have to write code in your function to parse through the arguements and determine what has been passed in.
Thanks for the response. FWIW, I have no problems with having both functions and cmdlets. In fact, I do think that script cmdlets are a great feature in V2. My question was purely about the syntax used to define the script cmdlets and more precisely about the syntax change coming in CTP3 (whether is a better syntax or not or how it could be improved).
Yeah, you have to just tell yourself that you technically never had a "cmdlet" keyword in scripts. Rembemer that even in CTP2, when you write "cmdlet" (or "filter" for that matter) … what you get is actually a Function that shows up in the Function: PSDrive.
If you think about it in terms of the change from v1 (and pretend not to be an alpha technology user for a moment), what they are doing is enhancing the existing v1 function feature with the ability to have attributes so we can specify constraints and order, and take values from the pipeline or from properties of the pipeline objects.
I’d agree that the Param([Parameter]…) syntax is a little redundant, but I can’t really think of a better pair of words to use, and they were going to have that redundancy even if they have the "cmdlet" keyword, so basically … no difference, right?