Understanding PowerShell syntax
Windows PowerShell uses a verb-noun pair as a naming system. The name of each cmdlet includes a standard verb hyphenated with a specific noun. Verbs are used for specific actions in Windows PowerShell, and nouns are used to describe objects by users and system administrators. Here are some of the examples that show how little effort is required to work with cmdlets, and one does not have to memorize them.
Nouns in a cmdlet describe what the cmdlet will act on. For example, get-process, get-service, stop-process and stop-service. If this is used regularly, one can get to know the standard set of verbs and nouns used in PowerShell. With these verb-noun pair, hundreds of cmdlet names can be remembered and used.
You will be able to recognize the operation performed by a cmdlet by just reading the name as it is mostly apparent about the name used by a new command should relate to the operation. For example, get-computer
will give you a list of computers on your network. Get-date
and get-time
will display the system date and time respectively. Similarly, stop-computer
will shut down a computer. Set-date
and set-time
will set the system's date and time.
Let's look at how to list all the commands that have the particular verbs called Get and Set using Get-command
with the –verb
parameter. The output of this command is piped to display in a table format (more on this will be covered later in the Usage of pipelines section).
In the next example, we will just use instead of a format table:
Now, let's use the -Noun
parameter, and we will find a list of commands that affect the same object type. For example, if you want to see commands that are available for managing computers, type the following command:
Traditionally, the commands in the earlier shells usually do not have a consistent parameter name. Sometimes, they don't have a name at all, and even if they do, they are abbreviated to make typing easier. But, this makes it difficult for new users to understand the parameters that are used. In Windows PowerShell, consistency is encouraged by using developer guidance on the usage of parameter names.
The names of the parameters in Windows PowerShell have a hyphen '-
' prepended to them to allow PowerShell to demarcate them as parameters. In the get-command –Noun
example, the name of the parameter is Noun and is typed as –Noun
.
The cmdlets in Windows PowerShell support several common parameters as the PowerShell engine controls these parameters. So, no matter how they are implemented, they will behave in a similar way. Another way to drive the consistency and learning of Windows PowerShell are some of the common parameters like—Confirm
, WhatIf
, outvariable
, warningaction
, warningvariable
, debug
, and verbose
.