There are two elements in a module manifest: cmdlet and function.
What is the difference between a cmdlet and a function?
A cmdlet -- pronounced command-let -- is a small, lightweight command that is used in the Windows PowerShell environment. A cmdlet typically exists as a small script that is intended to perform a single specific function such as coping files and changing directories.
One of the differences between a function and an advanced function is that advanced functions have a number of common parameters that are added to the function automatically. These common parameters include parameters such as Verbose and Debug.
Cmdlets are lightweight commands used in the PowerShell environment. Most of the cmdlets in PowerShell use the Verb-Noun format. For example, Get-Command, Update-Help, Start-Service, etc.
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.
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 (binary) 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:
You gain automatic support for common parameters such as -Verbose
, and -OutVariable
and, on an opt-in basis, for -WhatIf
and -Confirm
.
Arguments that cannot be bound to explicitly declared parameters result in an error on invocation.
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
.
Unfortunately, even advanced functions and cmdlets aren't created fully equal:
Advanced functions run in a child variable scope, unlike cmdlets.
$PSCmdlet.SessionState.PSVariable
object, as shown in this answer.Advanced functions apply culture-invariant parameter conversions, unlike cmdlets.
Advanced functions, in Windows PowerShell, handle ValueFromRemainingArguments
differently than cmdlets.
A simple function, by contrast:
$args
variable; see this answer for how to prevent passing unbound (unexpected) arguments.$Input
or even via a process { ... }
block, if desired.Set-Variable
with -Scope 1
.Filter
keyword. Its body is implicitly invoked for each pipeline input object, reflected in automatic variable $_
.While exporting functions as part of a module - preferably via its module manifest (*.psd1
) - doesn't enforce that functions are advanced ones, it is good practice to only export advanced functions.
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.
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