Cmdlets vs Functions
A discussion came up recently in an internal mailing list about the difference between Cmdlets and Functions. What was most interesting was how many hard and fast distinctions and rules came out of the discussion, even though those distinctions don’t really exist.
The key point is that there really isn’t a hard and fast philosophical distinction between cmdlets and functions.
Most differences largely exist because of setup, installation, and supported features. In general, cmdlets (and snapins) are the best way to distribute features, while functions are the easiest way to implement them.
- It is currently much easier for ISVs and developers to package and deploy cmdlets than it is to package libraries of functions or scripts.
- It is currently easier to write and package help for cmdlets.
- 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. On the minus side, this makes you pay the ‘compilation’ tax — making it slower to implement and evaluate new functionality.
- In V1, Cmdlets provide the author a great deal of support for parameter validation, and tentative processing (-WhatIf, -Confirm.) This is an implementation artifact, though, and could go away in the future.
- [Various technical points] Functions support scoping, different naming guidelines, management through the function drive, etc. See your favourite scripting reference for these details.
So, really, the differences are really a function of water finding its level – people tend to one or the other based on what we’ve currently made easiest. Aside from the implementation language, all of these factors are transient, though, and could change at any time.