PowerShell Command Guide for newbies

PowerShell Command Guide for newbies

What is a PowerShell Command (cmdlets)?

A PowerShell command (cmdlet) is a lightweight command that is used in the Windows PowerShell environment.

When you execute a PowerShell command, the Windows PowerShell invokes (calls) the command (cmdlets). Cmdlets are normally invoked within a script.

Though you can run a PowerShell command or cdmlet individually, a Powershell command is more useful when used in a script for task automation.

Format of a PowerShell Command (cmdlet) format

A PowerShell Command – pronounced ‘Command-Lets’) take a Verb-Noun format. A cmdlet begins with a Verb followed by hyphen (-) then followed by the cmdlet Noun.

The Verb portion normally depict an action. An example of a PowerShell cmdlet Verb is the word ‘Get’. Another example is the word ‘Set’.

A cmdlet Noun is the name of the object in which an action is to be performed. A PowerShell cmdlet Noun may not necessarily be an English language noun.

For example a cmdlet Noun is the word ‘ADUser’. As you can see, ‘ADUser’ is not an English noun but it is a Powershell cmdlet noun. Another example of a cmdlet Noun is the word ‘Date’.

Putting it together, a real example of a PowerShell command (cmdlet) is the Get-Date cmdlet. Another example is the Get-Process cmdlet.

In both examples, ‘Get’ is the action word (verb) while ‘Date’ and ‘Process’ are the objects on which the action is performed (noun).

The beautiful thing about PowerShell commands is their Verb-Noun format. This makes it easy for you to remember them.

It is especially useful for non-English speakers. A Powershell command do not use machine languages; a Powershell command uses human-friendly, easy-to-remember Verb-Noun format

How to find a PowerShell command using the Get-Command cmdlet

I said earlier that the Verb-Noun format of a PowerShell command makes it easy to remember them. Fortunately, you don’t necessarily have to remember the name of PowerShell command! The Get-Command cmdlet helps you find any cmdlet.

To list all PowerShell commands available on your computer, execute the command below

Get-Command

This will return a long list of PowerShell commands as shown below:

powershell-command-5

Figure 1.0

The result of the previous command may not be very useful as the list is just too long! To find a specific PowerShell command using the Get-Command, type Get-Command followed by a portion or full name of the cmdlet.

Notice the three columns in the figure above? Three CommandTypes are listed: Alias, Function or Cmdlet. The ‘Name’ column has the name of the PowerShell Command. Lastly, there is the ‘ModuleName’ column. This is the module the Module the cmdlet, function or alias belongs to.

Important

The commands shown in this Tutorial are executed on Windows PowerShell 5.0 (Windows 10). In PowerShell 5.0, ‘ModuleName’ column changed to ‘Source’ column. It also includes a ‘Version’ column. If you are using PowerShell 4.0 (Windows 8.1) or PowerShell 3.0 (Windows 7), yours will have the ‘ModuleName’ column.

How to use the Get-Command cmdlet

Before you use the Get-Command cmdlet to find information about a PowerShell command, you should have some idea what the command you want looks like. This is necessary except you wish to list all commands.

Having an idea about a PowerShell command you want information about is usually easy because of the Verb-Noun format of cmdlets. For example, to find a PowerShell command that begins with the word ‘compare’, execute the command below:

Get-Command compare*

This will return all powershell commands beginning with the verb ‘compare’ as shown in figure 2.0 below:

powershell-command

Figure 2.0

If you wish to find PowerShell commands that begins with the letters ‘AD’, type the command:

Get-Command AD*

Result is shown in figure 3.0 below:

powershell-command-1

Figure 3.0 – some results not shown

The last two examples demonstrate that the Get-Command cmdlet supports wild-cards. As seen in both examples, I used the asterisk (*) wild-card. Most Powershell commands support wild-cards.

PowerShell command parameters

Most PowerShell commands (cmdlets, functions and scripts) will have some parameters. Parameters allow PowerShell commands to accept inputs from users. Parameters give you, the user control over the result of a PowerShell command. PowerShell commands parameters have the format:

Verb-Noun -Parameter_Name input

‘Verb-Noun’ represents the name of a PowerShell cmdlet or function; for example Get-Date. In the format example shown above, ‘-Parameter_Name’ is the name of the parameter defined in the PowerShell cmdlet. ‘input’ is the value entered by the user. A good example is:

Get-Date -Format g

See figure 4.0 for the output of the above powershell command:

powershell-command-2

Figure 4.0

In the above example, ‘Get-Date’ is the Powershell cmdlet, ‘-Format’ is the parameter name and the letter ‘g’ is your input. The command gets the current date and time and formats it in short-date and short-time format. It uses the .NET Framework “g” format specifier (General [short date and short time]) to specify the format. See the online help information for the Get-Date cmdlet.

You can also find a PowerShell command by Module. To find a PowerShell command by module, execute the command below:

Get-Command -Module PKI

PKI is the ModuleName. Refer to figure 1.0 above. Notice that the result contains PowerShell Commands (Functions and Cmdlets) that belong to the PKI Module. See Figure below for details. Note that mine returns “Source” instead “ModuleName”. As I mentioned ealier, this is because I am using PowerShell version 5.0 on Windows 10.

powershell-command-6

Figure 5.0

Get-Command and cmdlet Verb-Nouns

You can combine the Get-Command cmdlet with your knowledge of PowerShell cmdlet Verb-Noun format. Let’s say you wish to find all PowerShell commands with the verb ‘out’, simply type:

Get-Command -Verb 'out'

The result of the previous command is shown below:

powershell-command-4

Figure 6.0

You can also find a PowerShell command by using the -Noun parameter of the Get-Command cmdlet. For example, to find all PowerShell commands with the noun ‘Date’, execute the command below:

Get-Command -Noun 'Date'

Getting help with the Get-Help command

Once you have found a PowerShell command, you need to find out how to use the command. You will need information like available parameters, syntaxes and examples. The Get-Help command will provide these information and more about a Powershell command. The format of the Get-Help command is shown below:

Get-Help Cmdlet name -Parameters

Like any other Powershell command, the Get-Help command has parameters that accept input from you and provides you the ability to control the output of the GetHelp Powershell command. For exampe, to get information about how to use the Get-Date cmdlet, type the command below:

Get-Help Get-Date

Result shown below:

powershell-command-3

Figure 7.0

The command above is the basic form of the use of the Get-Help command. Below is a list of the Get-Help parameters that might be very helpful to you:

-Detailed, -Full, -Online and -Examples.

To see this in action, try your hand on the following Powershell commands:

Get-Help Get-Date -Detailed

Get-Help Get-Date -Full

Get-Help Get-Date -Online

Get-Help Get-Date -Examples

For more information and further reading, see the references below

References and further reading

Leave a Reply