What is PowerShell Cmdlet?
A cmdlet ‘pronounced as a command-lets‘ is a lightweight command which is used in the PowerShell environment. These are the special commands in the PowerShell environment which implements the special functions. The cmdlets follow a ‘verb-noun‘ pattern, such as ‘set-childItem‘.
Windows PowerShell runtime invokes these commands in the context of automation scripts which are provided at the command line. It also invokes them programmatically through Windows PowerShell Application Programming Interfaces APIs.
The cmdlets in a PowerShell perform an action and returns a Microsoft .NET framework object to the next command in the pipeline. Cmdlets can receive objects as input and can also output their results as objects, which makes them suitable for use as recipients in a pipeline.
If you write a cmdlet, you must implement a cmdlet class which derives from one of two specialized cmdlet base classes. A derived class must:
- Declare an attribute which identifies the derived class as a cmdlet.
- Define the public properties which are decorated with the attributes that identify the public properties as parameters of a cmdlet.
- To process records, it overrides one or more of the input processing methods.
An assembly can be loaded which contains the class directly by using an Import-Module cmdlet, or we can create a host application which loads the assembly by using a System.Management.Automation.Runspaces.Initialsessionstate API. Both methods provide programmatic and command-line access to cmdlet functionality.
The terms which are used frequently in the Windows PowerShell cmdlet are as follows:
Windows PowerShell defines several attributes which are used to add common functionality to the cmdlets. It includes the cmdlet attribute which identifies a Microsoft .NET framework class as a cmdlet class.
It provides the mechanism which allows the cmdlet to access the input. Parameters can accept input directly from the command line, or the objects passed to the cmdlet through the pipeline.
A group of parameters are used in the same command to perform a particular action. A cmdlet in a PowerShell can have multiple parameter sets, but each parameter set must have at least one unique parameter.
Windows PowerShell uses parameter sets allow you to write a single cmdlet which can do different actions for different scenarios. Parameter sets will enable you to expose different parameters to a user, and based on the parameters; it returns different information specified by the user.
Dynamic parameters are added to the cmdlet at runtime. Typically, when the argument of another parameter is set to a particular value, then these arguments are added to cmdlet.
Input Processing method
Input processing methods allow the cmdlet for performing the operations of input processing, pre-processing, and post-processing. Those methods that a cmdlet uses to process a record it receives as an input. The input processing method includes the System.Management.Automation.Cmdlet.EndProcessing method, System.Management.Automation.Cmdlet.ProcessRecord method, and System.Management.Automation.Cmdlet.BeginProcessing method.
The logical group of commands are treated as a single-task. The task automatically fails, if any of the command in the group fails. And a user has the choice to reject or accept the actions performed within the transactions. Support to transactions was introduced in Windows PowerShell version 2.0.
How Cmdlet differ from commands
The cmdlets differ from the commands in other command shell environments in the following ways:
- Generally, commands do not support their parsing, output formatting, or error presentation. The Windows PowerShell runtime handles these three (parsing, output formatting, or error presentations).
- The cmdlets process the input objects from a pipeline rather than from stream of characters, and cmdlets deliver output objects to the pipeline.
- Cmdlets process a single object at a time, so they are record-oriented.
- Cmdlets are the instances of .NET framework classes, so they are not stand-alone executables.