How to use the command 'Get-Command' (with examples)

How to use the command 'Get-Command' (with examples)

The ‘Get-Command’ command is used to list and get available commands in the current PowerShell session. It provides a way to easily discover the various commands (aliases, cmdlets, functions) that can be used in PowerShell.

Use case 1: List all available PowerShell commands in the current computer

Code:

Get-Command

Motivation: This use case is useful when you want to see the full list of available PowerShell commands in the current computer. It helps in discovering new commands that can be used to perform various tasks within PowerShell.

Explanation: The command ‘Get-Command’ without any arguments lists all the available PowerShell commands in the current computer.

Example output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           %                                                  -> For
Alias           ?                                                  -> Where-Object
Alias           ac                                                 -> Add-Content
...
Cmdlet          Write-Error                                        3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Write-Host                                         3.1.0.0    Microsoft.PowerShell.Utility
...
Function        Exit-PSSession                                     3.1.0.0    Microsoft.PowerShell.Core
Function        Expand-Archive                                     3.1.0.0    Microsoft.PowerShell.Archive
...

Use case 2: List all available PowerShell commands in the current session

Code:

Get-Command -ListImported

Motivation: This use case is useful when you want to see the list of available PowerShell commands that are imported in the current session. It helps in seeing the specific commands that are currently available for use.

Explanation: The ‘-ListImported’ parameter filters the list of commands returned to only include the commands that are imported in the current session.

Example output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ?                                                  -> Where-Object
Alias           ac                                                 -> Add-Content
...
Cmdlet          Write-Error                                        3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Write-Host                                         3.1.0.0    Microsoft.PowerShell.Utility
...
Function        Get-Date                                           3.1.0.0    Microsoft.PowerShell.Utility
Function        Get-ExecutionPolicy                                3.1.0.0    Microsoft.PowerShell.Security
...

Use case 3: List only PowerShell aliases/cmdlets/functions available in the computer

Code:

Get-Command -Type Alias|Cmdlet|Function

Motivation: This use case is useful when you want to see the specific types of PowerShell commands available in the computer, such as aliases, cmdlets, or functions. It helps in narrowing down the list of commands based on their types.

Explanation: The ‘-Type’ parameter is used to filter the list of commands returned based on their type. In this example, the parameter is set to ‘Alias|Cmdlet|Function’, which means only aliases, cmdlets, and functions will be included in the result.

Example output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           %                                                  -> For
Alias           ?                                                  -> Where-Object
Alias           ac                                                 -> Add-Content
...
Cmdlet          Write-Error                                        3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Write-Host                                         3.1.0.0    Microsoft.PowerShell.Utility
...
Function        Get-Host                                           3.1.0.0    Microsoft.PowerShell.Utility
Function        Get-Process                                        3.1.0.0    Microsoft.PowerShell.Management
...

Use case 4: List only programs or commands available on PATH in the current session

Code:

Get-Command -Type Application

Motivation: This use case is useful when you want to see the list of programs or commands available on the PATH in the current session. It helps in identifying the executables that can be run directly from the command line.

Explanation: The ‘-Type’ parameter is used to filter the list of commands returned based on their type. In this example, the parameter is set to ‘Application’, which means only programs or commands available on the PATH will be included in the result.

Example output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     Add-MpPreference                                  4.18...    C:\Program Files\Windows Defender\MSASCui.exe
Application     diskpart.exe                                      10.0...    C:\Windows\system32\diskpart.exe
...

Use case 5: List only PowerShell commands by the module name

Code:

Get-Command -Module module

Motivation: This use case is useful when you want to see the specific PowerShell commands provided by a particular module. It helps in understanding the commands available for a specific purpose or functionality.

Explanation: The ‘-Module’ parameter is used to filter the list of commands returned based on the module name. In this example, the parameter is set to ‘module’, which should be replaced with the actual name of the module you want to filter by.

Example output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Add-PSSnapin                                       3.1.0.0    Microsoft.PowerShell.Core
Cmdlet          Debug-Process                                      3.1.0.0    Microsoft.PowerShell.Utility
...

Use case 6: Get the command information by its name

Code:

Get-Command command

Motivation: This use case is useful when you want to get specific information about a command, such as its version number or module name. It helps in understanding the details of a command for better usage.

Explanation: By providing the name of the command as an argument, you can retrieve its information. The command name should be replaced with the actual name of the command you want to get information about.

Example output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-Command                                        3.1.0.0    Microsoft.PowerShell.Core

Conclusion:

The ‘Get-Command’ command is a powerful tool for discovering the available commands in PowerShell. It allows you to explore the different types of commands, filter them based on various criteria, and retrieve detailed information about specific commands. By leveraging the ‘Get-Command’ command, you can efficiently navigate and utilize the vast number of commands available in PowerShell.

Related Posts

How to use the command pgbench (with examples)

How to use the command pgbench (with examples)

The pgbench command is used to run benchmark tests on PostgreSQL.

Read More
Using megatools-dl (with examples)

Using megatools-dl (with examples)

Megatools-dl is a command-line tool that allows users to download files from the mega.

Read More
exiv2 (with examples)

exiv2 (with examples)

1: Print a summary of the image Exif metadata To print a summary of the image Exif metadata, you can use the following command:

Read More