CmdletBinding()
In PowerShell Version 1.0, the only way to write cmdlets was with managed code. Starting with Version 2.0, it became possible to write advanced functions that have all of the capabilities of managed cmdlets but are written in 100 percent PowerShell. The key to writing advanced functions (also sometimes called script cmdlets) is the CmdletBinding()
attribute, which is added to the Param()
statement. Since the attribute is tied to the Param()
statement, advanced functions must have a Param()
statement even if they have no parameters. In this case, an empty Param()
statement can be used. The following is an example of a normal function and an advanced function, which are nominally the same:
#this is a normal function function add-item{ param($x,$y) Write-Output $x+$y } #the same function as an advanced function function add-itemAdv{ [CmdletBinding()] param($x,$y) Write-Output $x+$y }
Common parameter support
Although the two functions seem to be the same, Get-Help
shows...