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.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.