Command naming and discovery
Commands in PowerShell are formed around verb and noun pairs in the form verb-noun.
This feature is useful when finding commands; it allows educated guesses about the names of commands so that there is little need to memorize long lists of commands. Commands use verbs to describe intent, and nouns to describe the target.
Verbs
The list of verbs is maintained by Microsoft. Verbs are words such as Add
, Get
, Set
, and New
. This formal approach to naming commands greatly assists in discovery.
The list of verbs can be seen in PowerShell using the following command:
Get-Verb
Verbs are grouped around different areas, such as data, life cycle, and security. Complementary actions such as encryption and decryption tend to use verbs in the same group; for example, the verb Protect
may be used to encrypt something and the verb Unprotect
may be used to decrypt something.
More commonly, Get
and Set
, or New
and Remove
commands may also be seen as complementary.
Verb descriptions
A detailed list of verbs, along with their use cases, is available on MSDN:
It is possible, although not recommended, to use verbs other than those in the approved list. If a command with an unapproved verb is written and included in a module, a warning message will be displayed every time the module is imported.
Verbs are paired with nouns that describe the target of a command.
Nouns
A noun provides a very short description of the object the command is expecting to act on. The noun part may be a single word, as is the case with Get-Process
, New-Item
, and Get-Help
, or more than one word, as seen with Get-ChildItem
, Invoke-WebRequest
, and Send-MailMessage
.
Command names often include a prefix on the noun. The Microsoft AD module uses the AD
prefix. The Microsoft Graph modules are prefixed with the Mg
prefix. Commands for managing the network components of the Windows operating system are prefixed with Net
.
Modules are explored in Chapter 2, Modules. As mentioned above, the commands used to manage the networking components of Windows use the Net
prefix on every noun. This, in turn, allows the use of wildcards to search for commands.
Finding commands
The verb-noun pairing strives to make it easier to find commands without resorting to search engines.
For example, if the goal was to list firewall rules, the following command may be used to show the Get
commands that might affect the firewall:
PS> Get-Command Get-*Firewall*
CommandType Name Version Source
----------- ---- ------- ------
Function Get-NetFirewallAddressFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallApplicationFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallInterfaceFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallInterfaceTypeFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallPortFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallProfile 2.0.0.0 NetSecurity
Function Get-NetFirewallRule 2.0.0.0 NetSecurity
Function Get-NetFirewallSecurityFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallServiceFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallSetting 2.0.0.0 NetSecurity
A wildcard might be used for the verb or the specific parameters for Verb
and Noun
might be used with Get-Command
:
Get-Command -Verb Get, Set -Noun *Firewall*
The Get-Help
command may also be used to find the list of commands above:
Get-Help Get-*Firewall*
As Get-Help
also searches for help content, it is slower to use to search than Get-Command
.
The list of commands returned may vary, depending on the modules installed on the computer.
From the preceding list, Get-NetFirewallRule
closely matches the requirement (to see a list of firewall rules) and should be explored. Notice how each of the commands in the list above maintains the same prefix. This is a common naming practice in PowerShell.
Once a potential command has been found, Get-Help
can be used to assess whether the command is suitable.
Aliases
An alias in PowerShell is an alternate name for a command. A command may have more than one alias. Unlike languages like Bash, an alias cannot include parameters.
The list of aliases can be viewed by using Get-Alias
. The first few aliases are shown in the following example:
PS> Get-Alias
CommandType Name
----------- ----
Alias % -> ForEach-Object
Alias ? -> Where-Object
Alias ac -> Add-Content
Alias cat -> Get-Content
Alias cd -> Set-Location
Get-Alias
may be used to find the command behind an alias:
Get-Alias dir
The aliases available change depending on the operating system. For example, PowerShell on Linux omits aliases such as ac
(Add-Content
), ls
(Get-ChildItem
), and cat
(Get-Content
).
Get-Alias
may also be used to find the aliases for any command:
PS> Get-Alias -Definition Get-ChildItem
CommandType Name Version Source
----------- ---- ------- ------
Alias dir -> Get-ChildItem
Alias gci -> Get-ChildItem
Alias ls -> Get-ChildItem
Examples of aliases that are frequently used in examples on the internet include the following:
%
forForEach-Object
?
forWhere-Object
cd
forSet-Location
gc
orcat
forGet-Content
ls
ordir
forGet-ChildItem
irm
forInvoke-WebRequest
iex
forInvoke-Expression
An alias does not change how a command is used. There is no difference in the result of the following two commands:
cd $env:TEMP
Set-Location $env:TEMP
New aliases are created with the New-Alias
command. For example, an alias named grep
for the Select-String
command can be created as follows:
New-Alias grep -Value Select-String
Aliases can be removed using the Remove-Alias
command, including default aliases such as ls
:
Remove-Alias grep
Aliases may also be removed using Remove-Item
as an alternative to Remove-Alias
:
Remove-Item alias:\grep
Aliases created in one session are not remembered when a new PowerShell session is started.
More information is available about aliases in the help file about_Aliases
. The help file is viewed using the following command:
Get-Help about_Aliases
As mentioned above, aliases do not persist between PowerShell sessions (when the console is restarted). Profile scripts can be used to make an alias (or any other preferences) available when PowerShell is restarted.