What does PowerShell's [CmdletBinding()] Do?

Folks are often confused about when to use this "decorator" in PowerShell scripts and functions. Let's clear up the confusion.

Don Jones

February 8, 2012

1 Min Read
ITPro Today logo

For more technical explainers on PowerShell, read our updated 2021 report: PowerShell 101: A Technical Explainer for IT Pros.

It's not unusual to see folks write PowerShell scripts and functions whose first line is [CmdletBinding()]. What's it do?

It's generally a big part of advanced functions, or what some folks call "script cmdlets." Basically, it turns on cmdlet-style parameter binding capabilities, either for a script or for a function. You really get four magical capabilities with it:

  • The ability to add [Parameter()] decorators to parameters - see "about_functions_advanced_parameters" in PowerShell for more detail. Technically, these can be used without adding [CmdletBinding()], but you almost always see them together.

  • The ability to use Write-Verbose and Write-Debug in your script or function, and have their output controlled by -Verbose and -Debug parameters of that script or function. You don't need to declare those parameters - [CmdletBinding()] adds them.

  • Your script or function picks up the other common parameters, too, like -EV and -EA (see "about_common_parameters")

  • The ability to have -whatif and -confirm added to your script or function, by specifying something like [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact=Medium)]. You still have to implement support for these switches by using $pscmdlet.ShouldProcess() in your script.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like