Mastering the 'Get-Command' PowerShell Command (with Examples)
- Windows
- December 17, 2024
The Get-Command
cmdlet is an essential utility in PowerShell that allows users to retrieve comprehensive lists of available commands, including aliases, cmdlets, functions, and applications accessible from the command line. This command serves as both a tool for discovery and a starting point for scripting or automating IT tasks.
Use case 1: Listing All Available PowerShell Commands on the Computer
Code:
Get-Command
Motivation:
When you are getting started with PowerShell or need an overview of the diverse range of commands available for use, Get-Command
without any parameters gives you a complete overview of all PowerShell commands accessible on your computer. It is particularly useful when you want to familiarize yourself with the command environment or prepare for scripting tasks.
Explanation:
The command lacks additional parameters, which means it will return all commands that are installed and accessible on the system, including aliases, cmdlets, functions, scripts, and applications.
Example Output:
CommandType Name Version Source
----------- ---- ------- ------
Alias % -> ForEach-Object
Alias ? -> Where-Object
Cmdlet Add-Content 7.0.0.0 Microsoft.PowerShell.Management
Function Write-Host
Application notepad.exe
...
Use case 2: Listing All Available PowerShell Commands in the Current Session
Code:
Get-Command -ListImported
Motivation:
For users who wish to focus their attention on a specific session context, this use case is critical. It allows users to see only those commands that have been imported and are readily available in the current PowerShell session, without listing the potentially overwhelming full set of commands installed on the machine.
Explanation:
The -ListImported
parameter filters the results, listing only those commands imported into the current session. This helps streamline your view and avoid clutter.
Example Output:
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-Process 7.0.0.0 Microsoft.PowerShell.Management
Alias % -> ForEach-Object
...
Use case 3: Listing Only PowerShell Aliases, Cmdlets, and Functions on the Computer
Code:
Get-Command -Type Alias, Cmdlet, Function
Motivation:
This example provides a more focused list of elements that are specifically related to PowerShell: aliases, cmdlets, and functions. It’s beneficial when you’re interested in scripting within PowerShell’s domain and don’t need details about applications and external executables.
Explanation:
The -Type
parameter is used here with specific arguments Alias
, Cmdlet
, and Function
. These arguments instruct PowerShell to filter the output to include only the selected types.
Example Output:
CommandType Name Version Source
----------- ---- ------- ------
Alias cd -> Set-Location
Cmdlet Start-Service 7.0.0.0 Microsoft.PowerShell.Management
Function Get-Help
...
Use case 4: Listing Only Programs or Commands Available on PATH in the Current Session
Code:
Get-Command -Type Application
Motivation:
When needing an understanding of which executable files are readily accessible from your system’s environment path, this use case is essential. It’s useful for checking available applications that you can call directly from the command line without full paths.
Explanation:
The -Type
parameter is used with the argument Application
. This specifies that PowerShell should filter and list only the programs or applications accessible through the system’s PATH variable.
Example Output:
CommandType Name Version Source
----------- ---- ------- ------
Application chrome.exe
Application code.exe
Application notepad.exe
...
Use case 5: Listing PowerShell Commands by Module Name
Code:
Get-Command -Module Microsoft.PowerShell.Utility
Motivation:
This use case is particularly useful for administrators who need to utilize or explore commands within a specific module. It assists in understanding what utility-specific commands the module Microsoft.PowerShell.Utility
offers.
Explanation:
The -Module
parameter filters results based on the specified module name, in this case, Microsoft.PowerShell.Utility
. This focuses the results solely on commands that belong to this module, providing a tailored view.
Example Output:
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-Random 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Measure-Object 7.0.0.0 Microsoft.PowerShell.Utility
...
Use case 6: Getting Command Information by Name
Code:
Get-Command Get-Process
Motivation:
In situations where you require detailed information about a specific command, this example is invaluable. It provides metadata such as the command’s version, source module, and exact syntax, which is crucial when integrating it into scripts or automation workflows.
Explanation:
The command name Get-Process
is provided directly as an argument to Get-Command
. This tells PowerShell to retrieve comprehensive details about that specific command.
Example Output:
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-Process 7.0.0.0 Microsoft.PowerShell.Management
Conclusion:
The PowerShell Get-Command
cmdlet provides powerful ways to explore and retrieve information about available commands within a PowerShell session or on a computer system. Each use case focuses on different aspects of command discovery, whether you’re looking at session-specific, machine-specific, or module-specific command variants. By mastering these use cases, users can harness the full potential of PowerShell for a wide range of administrative and scripting tasks.