PowerShell Command
PowerShell Commands, also called Commandlets or Cmdlets, are the smallest unit of the PowerShell scripting language, consisting of a verb, a noun and a parameter list.
Definition
PowerShell Commands, also called Commandlets or Cmdlets, are the smallest unit of the PowerShell scripting language, consisting of a verb, a noun and a parameter list.
PowerShell Commands are based on structural verb-noun name conventions, same goes for their derived Microsoft .NET Framework classes. The name convention with two components exponentially increases the number of possible combinations while reducing the number of base commands one must know thus supporting the main idea of PowerShell being a rather easy and literal scripting language.
In this article the basic structure of these commands will be recalled with a few examples and Verb Naming Rules will be presented in a brief form. To get a general overview about the PowerShell scripting basics, visit the article on PowerShell Scripting Language.
Structure of PowerShell Commands
Cmdlets
PowerShell Cmdlets are single, light-weight commands. They can also be invoked by their regarding aliases. They serve two basic functions: perform an action and return an object to a pipeline.
Pipeline
Similar to real pipelines, pipelines in PowerShell connect different segments and what is entering is exiting. Controlled by the pipe operator „|“ does exactly that: the output of the first command operates as the input of the subsequent command. Despite PowerShell showing text on-screen, PowerShell does pipe objects and not texts between commands.
Verb-Noun
Or, verbalized: PowerShell commands obey a Do-AThing structure, with a couple of effects: on the one hand, modular commands are easier to remember than individual commands themselves, on the other hand it allows cross inference and stretches the limit of known commands even further. Described descriptively: If a known Noun works with let us say the Verb „Get“, chances are high it works with „Set“ too, even if unknown yet.
Approved Verbs for PowerShell Commands
General guidelines
Verb Naming Rules are quite important for commands: using predefined verb names provided by PowerShell and refining the action with the use of parameters ensure consistency in naming, the same goes for using Pascal Casing (capitalized initial letters).
A certain alias prefix is signed to each approved verb. It is used to name aliases for commands using that verb. This though, must not be respected for command aliases mimicking well known PowerShell commands from other environments.
For a full list of PowerShell approved verbs/aliases, see Microsoft Docs‘ list of Approved Verbs for PowerShell Commands.
Alternatively you can use the command Get-Verb to have PowerShell put out a list of all approved verbs as well as their type.
Most types of Verbs
-
Common Verbs
The VerbsCommon Class is defined for a broad range of actions and most literally represents common cmdlets.
-
Communication Verbs
The VerbsCommunication Class is defined for verbs that are mostly used if the cmdlet communicates in some sort
-
Data Verbs
The VerbsData Class is defined for verbs that manipulate data in some sort
-
Diagnostic Verbs
The VerbsDiagnostic Class is defined for verbs that diagnose the health status in some sort
-
Lifecycle Verbs
The VerbsLifecycle Class is defined for verbs that manipulate the lifecycle of something in some sort
-
Security Verbs
The VerbsSecurtiy Class is defined for verbs that secure a resource in some sort
Parameters for PowerShell Commands
Parameters can be used on cmdlets, functions, and scripts to provide additional input by the user. As for a PowerShell command, they mainly extend or narrow down the effect of it.
In general, parameters have in common modifying a command, but their approach and effect differ, e.g., so-called switch parameter do not require a value. (If specified, the value is true, otherwise false).
CommonParameters specifies a set of cmdlet parameters that can be used on any cmdlet, as they are implemented by PowerShell and not the cmdlet provider/developer, making them runnable on all PowerShell Commands. Keep in mind that running into no errors does not equal results of value as some CommonParameters have no effect on some cmdlets.
Parameters can be used for much more though. In an AdvancedFunction, parameters affect the function and with arguments limit the parameter values that function users submit with the parameter. CommonParameters are also available on advanced functions that make use of one of the attributes „CmdletBinding„ or „Parameter„.