Loïc MICHEL
Loïc MICHEL

Reputation: 26160

What is [cmdletbinding()] and how does it work?

According to get-help about_Functions_CmdletBindingAttribute

The CmdletBinding attribute is an attribute of functions that makes them operate like compiled cmdlets

We can use it on the top of our scripts.

What is the function in this case?
An internal implicit "main" function called by the PowerShell engine for all its inputs?

Regarding this syntax:

[CmdletBinding(ConfirmImpact=<String>,
               DefaultParameterSetName=<String>,
               HelpURI=<URI>,
               SupportsPaging=<Boolean>,
               SupportsShouldProcess=<Boolean>,
               PositionalBinding=<Boolean>
               )]

what are we doing?
Instantiating a cmdlbinding object and passing an argument list to its constructor?

This syntax can be found in param() - for example: [Parameter(ValueFromPipeline=$true)].
Does this syntax have a particular name, and can it be found elsewhere?

Lastly, are we able, as simple PowerShellers, to mimic this functionality and modify the behavior of scripts by setting an attribute?

Upvotes: 76

Views: 104911

Answers (3)

Frode F.
Frode F.

Reputation: 54891

CmdletBinding, Parameter etc. are special attribute classes that scripters can use to define PowerShell's behavior, e.g. make a function an Advanced function with Cmdlet capabilites.

When you call them via e.g. [CmdletBinding()] you initialize a new instance of the class.

Read more about the CmdletBindingAttribute class at: MSDN

Read more about the ParameterAttribute class at: MSDN

More about Attribute classes here and here

Upvotes: 26

Shay Levy
Shay Levy

Reputation: 126792

Generally speaking, CmdletBinding is what makes a function into an Advanced function. Putting it at the top of a script makes the script an "advanced" script. Functions and scripts are much the same, where the script file name is equivalent to the function name and the script content is equivalent to the scriptblock section of a function.

CmdletBinding attributes give you control over function capabilities, such as adding Confirm and WhatIf support (via SupportsShouldProcess), Disable parameters positional binding, and so on.

Upvotes: 32

Emperor XLII
Emperor XLII

Reputation: 13442

Regarding the syntax question, the format closely matches how you apply a .NET attribute class to a member using named parameters in C#.

Compare the (simplified) grammar for attributes from section B.2.4 of The PowerShell Language Specification with that from section C.2.13 of the C# Language Specification:

B.2.4 Attributes   (PowerShell)

attribute:
  [ attribute-name ( attribute-arguments ) ]

attribute-arguments:
  attribute-argument
  attribute-argument
, attribute-arguments

attribute-argument:
  simple-name
= expression


C.2.13 Attributes   (C#)

attribute:
  [ attribute-name ( named-argument-list ) ]

named-argument-list:
  named-argument
  named-argument-list
, named-argument

named-argument:
  identifier
= attribute-argument-expression



I agree it might have been nice from a sense of conceptual brevity to e.g. re-use hashtable initialization syntax for attribute initialization. However, I can imagine supporting all the options from hashtables (like [A(P=v)] and [A('P'=v)] and $n = 'P'; [A($n=v)] and such, or some particular subset of them) just to use ; as the separator character would have been more trouble than it was worth.

On the other hand, if you want to use advanced functions, then maybe it makes sense to learn an advanced syntax :)

Upvotes: 4

Related Questions