Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 846
Loïc MICHEL Avatar asked Feb 03 '13 09:02

Loïc MICHEL


People also ask

What is CmdletBinding ()] in PowerShell?

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.

When should I use CmdletBinding?

You can use the CmdletBinding attribute to add basic cmdlet features, such as common parameters, to functions or to make certain methods available that allow you to change the behavior of the function.

What is the CmdletBinding attribute?

The CmdletBinding attribute is an attribute of functions that makes them operate like compiled cmdlets written in C#. It provides access to the features of cmdlets. PowerShell binds the parameters of functions that have the CmdletBinding attribute in the same way that it binds the parameters of compiled cmdlets.

What is the use of the SupportsShouldProcess =$ true inside the CmdletBinding () attribute?

The -Confirm parameter is automatically added when using SupportsShouldProcess in the CmdletBinding . If the user specifies -Force , we want to suppress the confirm prompt unless they also specify -Confirm . This allows a user to force a change but still confirm the change.


2 Answers

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

like image 169
Frode F. Avatar answered Oct 08 '22 15:10

Frode F.


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.

like image 26
Shay Levy Avatar answered Oct 08 '22 15:10

Shay Levy