Get Command - PowerShell


Overview

The Get-Command cmdlet retrieves information about commands, functions, scripts, and aliases available in the current PowerShell session. It allows you to explore and discover the available commands and their properties, which can be useful for finding specific commands, understanding command syntax, and debugging scripts.

Syntax

Get-Command [-Name] <string[]>
           [[-Type] <string[]>]
           [[-Syntax]]
           [[-Namespace] <string[]>]
           [[-CommandType] <string[]>]
           [[-Module] <string[]>]
           [[-Parameter] <string[]>]
           [[-Definition] [<string>| <scriptblock>]]
           [[-All] [<switch>]]
           [[-Paged] [<switch>]]
           [[-ErrorAction] <ActionPreference>]
           [[-Debug] [<switch>]]
           [[-Verbose] [<switch>]]
           [[-OutFile] <string>]
           [[-OutVariable] <string>]
           [<CommonParameters>]

Options/Flags

  • -Name: Specifies the name or wildcard pattern of the commands you want to retrieve.
  • -Type: Filters the retrieved commands by type (e.g., Cmdlet, Function, Script, Alias).
  • -Syntax: Displays the full command syntax, including all parameters and their types.
  • -Namespace: Restricts the search to specific namespaces.
  • -CommandType: Filters the retrieved commands by their command type (e.g., Application, Cmdlet, ExternalScript).
  • -Module: Only retrieves commands that are exported from specified modules.
  • -Parameter: Filters the retrieved commands by the presence of specific parameters.
  • -Definition: Outputs the definition of the specified command. Can be a string or a script block to customize the output.
  • -All: Retrieves all commands, including those that are not visible in the current session.
  • -Paged: Displays the results in pages for easier navigation.
  • -ErrorAction: Specifies the action to take when an error occurs.
  • -Debug, -Verbose: Enables additional debug and verbose output.
  • -OutFile, -OutVariable: Redirects the output to a file or variable.

Examples

Get all commands in the current session:

Get-Command

Find commands related to networking:

Get-Command -Name *net*

Get detailed syntax for a specific command:

Get-Command -Name New-Item -Syntax

Retrieve commands in the “Microsoft.PowerShell.Management” module:

Get-Command -Module Microsoft.PowerShell.Management

Find commands that accept a “Path” parameter:

Get-Command -Parameter Path

Common Issues

  • Command not found: Ensure that the command you are trying to find is installed and available in the current session.
  • Excessive output: Use -Paged or -All sparingly to avoid overwhelming your console.
  • Incorrect or incomplete results: Check your search criteria and make sure it correctly matches the desired commands.

Integration

Get-Command can be combined with other PowerShell commands to perform advanced tasks, such as:

  • Building custom command lists: Get-Command | Where-Object { $_.CommandType -eq "Cmdlet" }
  • Creating custom help: Get-Command -Name Get-* | Format-Table -AutoSize
  • Automating command discovery: Get-Command -All -Module\* -OutFile command-inventory.txt
  • Get-Member
  • Where-Object
  • Help