- Poll: How reliable are ChatGPT and Bing Chat? - Tue, May 23 2023
- Pip install Boto3 - Thu, Mar 24 2022
- Install Boto3 (AWS SDK for Python) in Visual Studio Code (VS Code) on Windows - Wed, Feb 23 2022
The number of PowerShell cmdlets has increased with each release, and you’ll now find few Windows functions or features that you can’t manage with PowerShell. However, you’ll often face the question of which cmdlet to use to get the job done. The Get-Command cmdlet offers various options to search for the available cmdlets on your computer.
To display a list of all available PowerShell cmdlets, you have to run Get-Command without parameters, like this:
Note that this command lists not only PowerShell cmdlets but also functions, workflows, and aliases. You can even use Get-Command to view all executables, including files that are not Windows PowerShell files, like this:
This command will search for all executables in all folders that are stored in the Path environment variable. You can list these folders by typing $env:path at a PowerShell prompt.
However, in most cases, you will only want to list all cmdlets. You can do so by using the CommandType parameter, like this:
Get-Command -CommandType Cmdlet
This list is still a bit long if you want to skim through the cmdlets to find the one you used last week but can’t recall its name. If you know that the cmdlet is part of a certain module, say the Active Directory module, you can restrict the list to just this module:
Get-Command -Module ActiveDirectory
This command will only produce output if you installed the Active Directory module. However, PowerShell comes with many pre-installed modules. As of PowerShell 3.0, PowerShell automatically imports installed modules when you run a cmdlet or access one with Get-Help and Get-Command. To display a list of all installed modules, you can run this command:
You can see the imported cmdlets of your current session with this command:
Get-Command -ListImported -CommandType Cmdlet
Note that this list changes when you access modules in your current session. For instance, if you listed the available cmdlets in the AppLocker module with Get-Command -Module AppLocker, the -ListImported parameter will then also display the cmdlets included in the AppLocker module because Get-Command automatically imports the AppLocker module into your current session.
If you added modules to your session but you only want to know what cmdlets are already imported at the start of a session, use this command to list only the cmdlets of the PowerShell Core modules:
Get-Command -CommandType Cmdlet -Module Microsoft*,PS*
You probably know that PowerShell cmdlets follow the naming convention verb-noun, where verb stands for an action that is applied to the object to which the noun refers. To get a list of all available verbs, you can use the Get-Verb cmdlet. This list won’t help you much in your search for your cmdlet; however, if you know that you are looking for a cmdlet to retrieve information, it will start with the verb Get:
Get-Command -Verb Get
Quite a few approved PowerShell verbs exist. In most cases, you will remember the verb if you used the cmdlet before. The above command then allows you to reduce your search list.
No Get-Noun cmdlet exists, but we can use Get-Command to list all nouns:
Get-Command -Noun *
The problem with this command is that the list also includes function names. We can’t use the CommandType parameter together with the Noun parameter because they belong to different parameter sets. That is, if you use the Noun parameter, Get-Command only returns cmdlets anyway, and using the CommandType parameter would therefore make no sense. If you only want to see a list of cmdlet nouns without the function names, you can use this command:
Get-Command -CommandType Cmdlet | Select Noun | Sort Noun
We can pipe the output to the Select-Object cmdlet because Get-Command returns objects and Noun is one of their properties. To get an alphabetically sorted list, we pipe it to the Sort-Object cmdlet.
However, in most cases, you will want to search for a noun that contains a certain string. For instance, if you are looking for a way to manage AppLocker with PowerShell, you could display a list of nouns that start with “applocker” by using “*” as a wildcard character:
Get-Command -Noun applocker*
Because PowerShell is case insensitive, we don’t have to worry about capital letters.
If you only want to have cmdlet names in your results list, you can use the CommandType and Name parameters together:
Get-Command -CommandType Cmdlet -Name *applocker*
The wildcard characters at the beginning and the end ensure that the command finds all cmdlets that contain the string “applocker.”
After you find your cmdlet, you can use Get-Command to get more information about the cmdlet’s usage. You can even use Get-Command to find out more about Get-Command.
Get-Command Get-Command | Format-List
As mentioned above, Get-Command returns an object that we can pipe to the Format-List cmdlet to display a formatted list of the object’s properties. This will give you information about the cmdlet’s syntax. However, if you need more detailed help, you should use the Get-Help cmdlet:
Just make sure to get help before use Get-Help.
I only discussed the most common usage scenarios of Get-Command. For a complete list of all parameters, you can either run Get-Help Get-Command or read its documentation online.
In my next post I will discuss Show-Command, another helpful tool that allows you to find cmdlets and learn how to use them.
Join the 4sysops PowerShell group!
Your question was not answered? Ask in the PowerShell forum!