cmdletbinding or parameter, for PowerShell Advanced Function?

You must have read that cmdletbinding attribute adds common parameters to your function like -debug, -verbose. Which then enables you to use write-verbose, write-debug in your script. Same is true if you use [parameter()] attribute in any of your parameter. Basically, using any of these attributes, or both together, recognizes your function as advanced function.

So, when to use both? Simple. When you need both. They both have more purposes than just making a function as advanced function. [parameter()] is used when your parameters really need advance parameter functionality like mandatory, validation checks etc. In case your function doesn’t need any advance parameter functionality, then you can just use

[cmdletbinding()]param()

If your function is already using [parameter()] then no need to add [cmdletbinding()] unless you need features added by [cmdletbinding()] like -confirm, -whatif. In this case, you have to use both.

4sysops has a good article here that you can refer for more details.

About Dinesh Sharma

Experienced system architect, programmer, and trainer. This blog is a way of giving back and helping the community. So feel free to ask a question or to leave a comment.