Discovering & Using PowerShell Cmdlets
What We'll Learn:
In this short guide we are going to learn how to use the get-command and get-help cmdlets to discover new cmdlets.
- We will start by learning what the get-command cmdlet is and how to use it.
- We will then learn how we can discover even more commands using wildcards.
- And finally, how to get information on what a cmdlet does and how it can be used, using get-help.
Let’s get started!
Discovering Cmdlets Using: Get-Command
Powershells list of tools and commands is almost inexhaustible, and often times you might need a refresher on a cmdlet, or might need to discover one anew to accomplish a specific task.
That’s where the Get-Command cmdlet comes in. Discovering new cmdlets becomes quite easy using the Get-Command cmdlet.
It can not only be used to find out new cmdlets but as the name suggests, it can be used to display every command available in powershell. That would include: cmdlets, aliases, functions, filters, scripts, applications and more.
Lets start by using the get-command cmdlet to display every available command. To so simply type Get-Command in your PowerShell console without any parameters.
A list of every available PowerShell command will immediately show up.
At the first column you will find the type of the command followed by its name, version and source.
In many cases you wight want to restrict your search to a command of a single type, for example, you might want to view just cmdlets. To do so we are going to use the -type parameter, here is what the command would look like:
Get-Command -Type Cmdlet
As you can see only cmdlets are displayed this time.
Narrowing Our Search Using Wildcards
While restricting our search with the -type parameter is great, we can take it a step further by using wildcards.
Wildcards are special symbols that take the place of characters or words. The most commonly used wildcard is the asterix.
Using wildcards, we can restrict our search to commands that have a specific word within their name.
For example, to list all the cmdlets related to services, use the following command:
Get-Command -Type Cmdlet *Service*
Replace the word “service” in the above command to display cmdlets related to a different item.
Using two wildcards, one at the beginning of our word and one at the end, we were able to ignore every character that was located before and after our word, effectively selecting only cmdlets that have our word somewhere within their name.
Wildcards are huge time-savers and are extraordinarily useful, and you should be making use of them, not just to discover cmdlets but for any otherwise tedious task.
Discovering Parameters Using: Get-Help
PowerShell cmdlets, functions and scripts, use parameters (otherwise known as arguments, options or switches) to make the passage of information easy or to allow users to provide some sort of input.
With a cmdlet for example, a parameter can be typed after it to make it function in a specific way.
Cmdlets can have a lot of parameters and remembering all of them along with their appropriate syntax is nearly impossible, that’s where the Get-Help cmdlet comes in.
As the name implies, Get-Help can not only be used to display the parameters of a cmdlet but also a wealth of valuable information related to your cmdlet, such as everything from what it is and what is function is, to whether it accepts piped objects, and if so, of what type.
To use it simply type get-Help followed by the name of a cmdlet.
At this point you might get an error about help files not being installed properly in your computer. If that is the case for you simply execute the Update-help cmdlet to install or update all of your help files.
This operation can take a while to complete.
The Update-Help cmdlet might occasionally through an error because for some of the URLs from which it downloads the help files might be invalid, if that the case for you, simply ignore the error, the majority of your help files will be installed regardless.
Now that the operation is over, upon running the get-help command once more, you should see a detailed description of the cmdlet.
We can get even more information by using the -detailed parameter, such as this example:
Get-Help Get-ChildItem -detailed
Or we can even view usage examples using the -examples parameter.
Get-Help Get-ChildItem -examples
Using this information, you will be able to get valuable information and usage examples for any PowerShell cmdlet without loosing your head over it.
Make use of a combination of both the get-command and get help cmdlets to easily discover new cmdlets and to figure out how you can use them.
- The Get-command cmdlet can not only be used to find out new cmdlets but as the name suggests, it can be used to display every command available in powershell. That would include: cmdlets, aliases, functions, filters, scripts, applications and more.
- Wildcards are huge time-savers and are extraordinarily useful, and you should be making use of them, not just to discover cmdlets but for any otherwise tedious task.
- To get more information on what a cmdlet does and how it can be used, make use of the Get-Help cmdlet along with the -examples or -detailed parameters.
You now know how to easily discover new cmdlets and learn how they can be used.
If you liked this short guide take a look at a few of our other posts related to PowerShell.
If you really liked it consider enrolling in our begginers course where you will learn the ins and outs of PowerShell.