Skip to content

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

Last updated on July 17, 2023

In PowerShell, the [CmdletBinding()] attribute is used to enable advanced parameter functionality for a function or script. When you add this attribute to your function or script, it enables a set of features commonly found in built-in cmdlets, providing consistent behavior and enhanced capabilities.

Here’s how [CmdletBinding()] works and what it enables:

  1. Parameter declarations: The attribute allows you to define parameters for your function using the [Parameter()] attribute. This gives you greater control over parameter properties like mandatory, default values, parameter sets, aliases, and more.
  2. Common parameters: The attribute automatically adds common parameters, such as -Verbose, -Debug, -ErrorAction, and -ErrorVariable, to your function or script. These parameters allow you to control the behavior of the function, such as enabling verbose output, setting error handling options, and assigning error variables.
  3. Pipeline input: By using the [Parameter(ValueFromPipeline)] or [Parameter(ValueFromPipelineByPropertyName)] attribute on function parameters, you can allow your function to accept input from the pipeline. This means you can pass objects to your function using the pipeline, making it more versatile and enabling easier composition of commands.
  4. Parameter sets: The [CmdletBinding()] attribute also allows you to define multiple parameter sets for your function. A parameter set is a way to define different combinations of parameters that can be used with your function. This gives you greater flexibility in how your function can be called and allows you to handle different scenarios or variations.
  5. Supports WhatIf and Confirm: By adding [CmdletBinding(SupportsShouldProcess)] to your function or script, you enable the -WhatIf and -Confirm parameters. -WhatIf allows you to simulate the execution of your function without actually performing any changes, while -Confirm prompts for confirmation before executing the function.

Here’s an example of using [CmdletBinding()] in a function:

function Invoke-Something {
    [CmdletBinding()]
    param (
        [Parameter(Position=0, Mandatory=$true)]
        [string]$Name
    )

    # Function code here
    # ...
}

By adding [CmdletBinding()] above the param block, you enable the advanced functionality provided by CmdletBinding. You can then define your parameters and utilize the features mentioned above within your function.

In summary, [CmdletBinding()] is a powerful attribute in PowerShell that enables advanced parameter handling, common parameters, pipeline input, parameter sets, and support for WhatIf and Confirm functionality. It helps make your functions and scripts more consistent, versatile, and aligned with the behavior of built-in cmdlets.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments