If I create a simple Powershell function e.g.
Function Hello {
[CmdletBinding( )]
Param (
[parameter()]
$Name
)
Begin{}
Process{
Write-Output "Hello $Name"
}
End{}
}
then use Get-Command to list it with Get-Command Hello
, the cmdlet is listed as 'CommandType' function. Why isn't it listed as 'CommandType' cmdlet?
When exporting from modules I've also found I have to use FunctionsToExport instead of CmdletsToExport.
It doesn't seem to affect the use of the functions, I'm just curious why they're listed like this.
Long description. A cmdlet is a single command that participates in the pipeline semantics of PowerShell. This includes binary cmdlets, advanced script functions, CDXML, and Workflows. Advanced functions allow you create cmdlets that are written as a PowerShell function.
Cmdlets are written in a compiled . NET language, while functions (and scripts) are written in the PowerShell language. On the plus side, this makes certain developer tasks (such as P/Invoke calls, working with generics) much easier in a cmdlet.
PowerShell uses a verb-and-noun name pair to name cmdlets. For example, the Get-Command cmdlet included in PowerShell is used to get all the cmdlets that are registered in the command shell.
A function is a list of PowerShell statements that has a name that you assign. When you run a function, you type the function name. The statements in the list run as if you had typed them at the command prompt. Functions can be as simple as: PowerShell Copy.
There potentially isn't much difference between a function and a cmdlet, but that depends on how much work you're willing to put into writing the function. Don Jones wrote an article on TechNet back when these first came out to highlight some of these differences.
These functions, written entirely in script, have the same capabilities as a “real” cmdlet written in C# or Visual Basic and compiled in Visual Studio. These advanced functions (they were originally called “script cmdlets” early in the v2 development cycle) help you write more flexible functions you can then use seamlessly alongside regular cmdlets.
...
The real difference between a mere function and a full cmdlet is that cmdlets support powerful parameter bindings. You can use positional parameters, named parameters, mandatory parameters, and even do basic parameter validation checks—all by simply describing the parameter to the shell.
The code example you've offered already begins to blur the lines between the two by allowing a host of addition parameters via [CmdletBinding()]
, and beginning to describe a brand new parameter called $Name
. For example, you could now use Write-Verbose
anywhere in that function, and call the -Verbose
flag to see those statements without having to do any additional work.
Functionally, the end results of a compiled cmdlet or a function written in powershell don't have to be very different at all -- it seems to be more a matter of distinguishing compiled cmdlets from scripted functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With