What is the difference between a cmdlet and a function?
What is the difference between a cmdlet and a function?
There are two elements in a module manifest: cmdlet and function.
What is the difference between a cmdlet and a function?
2 Answers
2
A cmdlet is a .NET class written in C# or other .NET language and contained in a .dll (i.e. in a binary module). A function is specified directly in PowerShell in a script, script module or at the command line. A module manifest may include both script and binary modules so the manifest needs to be able to export both cmdlets and functions. It's even possible to have both a cmdlet and a function with the same name exported from a single manifest though that's generally not recommended.
It’s worth also highlighting that many community modules provide cmdlets that are written as functions but that behave (from a user perspective) exactly like an actual cmdlet described above.
– Mark Wragg
Aug 18 at 20:10
To complement Bruce Payette's helpful answer:
Not all functions are created equal in PowerShell:
An advanced function is the written-in-PowerShell analog of a cmdlet (which, as stated, is compiled from a .NET language); decorating a function's param(...)
block with the [CmdletBinding()]
attribute or decorating at least one parameter with a [Parameter()]
attribute thanks, Ansgar Wiechers
is what makes it an advanced one; as such, it supports certain standard behaviors:
param(...)
[CmdletBinding()]
[Parameter()]
-Verbose
-OutVariable
-WhatIf
-Confirm
Typically, but not necessarily, advanced functions support one-by-one pipeline-input processing via a process ...
script block, via parameter-binding parameters decorated with ValueFromPipeline
and/or ValueFromPipelineByPropertyName
.
process ...
ValueFromPipeline
ValueFromPipelineByPropertyName
Unfortunately, even advanced functions and cmdlets aren't created fully equal:
ValueFromRemainingAguments
A simple function, by contrast:
$Input
process ...
Filter
$_
While exporting functions as as part of a module - preferably via its module manifest (*.psd1
) - doesn't enforce that functions be advanced ones, it is good practice to only exported advanced functions.
*.psd1
I know it's a different question, but I wanted to ask it here: is there a difference in the name of the functions for which
SupportsShouldProcess=$true
and SupportsShouldProcess=$false
– mazzy
Aug 18 at 21:21
SupportsShouldProcess=$true
SupportsShouldProcess=$false
@mazzy: No, there are no specific names for these sub-types of advanced functions, but those that do implement
SupportsShouldProcess
gain support for the -WhatIf
and -Confirm
common parameters.– mklement0
Aug 18 at 23:34
SupportsShouldProcess
-WhatIf
-Confirm
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Got it. Thanks a lot.
– mazzy
Aug 18 at 20:03