Get-Command | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-Command. 

When to use Get-Command? 

The Get-Command cmdlet gets all commands that are installed on the computer, including cmdlets, aliases, functions, filters, scripts, and applications. Get-Command gets the commands from PowerShell modules and commands that were imported from other sessions. To get only commands that have been imported into the current session, use the –ListImported parameter.

Get-Command * gets all types of commands, including all of the non-PowerShell files in the Path environment variable ($env:Path), which it lists in the Application command type.

Using Get-Command with the exact name of the command, without wildcard characters, automatically imports the module that contains the command so that you can use the command immediately. To enable, disable, and configure automatic importing of modules, you can use the $PSModuleAutoLoadingPreference preference variable.

NOTE: Get-Command gets its data directly from the command code, unlike Get-Help, which gets its information from help topics.

Starting in Windows PowerShell 5.0, results of the Get-Command cmdlet display a Version column by default. A new Version property has been added to the CommandInfo class.

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Get-Command?

Get cmdlets, functions, and aliases:

Get-Command

This command gets the PowerShell cmdlets, functions, and aliases that are installed on the computer.

Get commands in the current session:

Get-Command -ListImported

This command uses the –ListImported parameter to get only the commands in the current session.

Get cmdlets and display them in order:

Get-Command -Type Cmdlet | Sort-Object -Property Noun | Format-Table -GroupBy Noun

This command gets all of the cmdlets, sorts them alphabetically by the noun in the cmdlet name, and then displays them in noun-based groups.

Get commands in a module:

Get-Command -Module Microsoft.PowerShell.Security, Microsoft.PowerShell.Utility

This command uses the –Module parameter to get the commands in the Microsoft.PowerShell.Security and Microsoft.PowerShell.Utility modules.

Get information about a cmdlet:

Get-Command Get-AppLockerPolicy

This command gets information about the Get-AppLockerPolicy cmdlet. It also imports the AppLocker module, which adds all of the commands in the AppLocker module to the current session.

Get the syntax of a cmdlet:

Get-Command Get-Childitem -Args Cert: -Syntax

This command uses the –ArgumentList and –Syntax parameters to get the syntax of the Get-ChildItem cmdlet when it is used in the Cert: drive. The Cert: drive is a PowerShell drive that the Certificate Provider adds to the session.

When you compare the syntax displayed in the output above, with the syntax that is displayed when you omit the –Args (ArgumentList) parameter, you will see that the Certificate provider adds a dynamic parameter, CodeSigningCert, to the Get-ChildItem cmdlet.


Get dynamic parameters:

function Get-DynamicParameters
{
param ($Cmdlet, $PSDrive)
(Get-Command $Cmdlet -ArgumentList $PSDrive).ParameterSets | ForEach-Object {$_.Parameters} | Where-Object { $_.IsDynamic } | Select-Object -Property Name -Unique
}
Get-DynamicParameters -Cmdlet Get-ChildItem -PSDrive Cert:

The command in the example uses the Get-DynamicParameters function to get the dynamic parameters that the Certificate provider adds to the Get-ChildItem cmdlet when it is used in the Cert: drive.

The Get-DynamicParameters function in this example gets the dynamic parameters of a cmdlet. This is an alternative to the method used in the previous example. Dynamic parameter can be added to a cmdlet by another cmdlet or a provider.

Get cmdlets by using a parameter name and type:

Get-Command -ParameterName *Auth* -ParameterType AuthenticationMechanism

This command gets cmdlets that have a parameter whose name includes Auth and whose type is AuthenticationMechanism.

The –ParameterType parameter distinguishes parameters that take an AuthenticationMechanism value from those that take an –AuthenticationLevel parameter, even when they have similar names.

Get cmdlets and functions that have an output type:

Get-Command -Type Cmdlet | Where-Object OutputType | Format-List -Property Name, OutputType

This command gets the cmdlets and functions that have an output type and the type of objects that they return.

The first part of the command gets all cmdlets. A pipeline operator (|) sends the cmdlets to the Where-Object cmdlet, which selects only the ones in which the OutputType property is populated.

Another pipeline operator sends the selected cmdlet objects to the Format-List cmdlet, which displays the name and output type of each cmdlet in a list.

The OutputType property of a CommandInfo object has a non-null value only when the cmdlet code defines the OutputType attribute for the cmdlet.

Get cmdlets that take a specific object type as input:

Get-Command -ParameterType (((Get-NetAdapter)[0]).PSTypeNames)

This command finds cmdlets that take net adapter objects as input. You can use this command format to find the cmdlets that accept the type of objects that any command returns.

The command uses the PSTypeNames intrinsic property of all objects, which gets the types that describe the object.

To get the PSTypeNames property of a net adapter, and not the PSTypeNames property of a collection of net adapters, the command uses array notation to get the first net adapter that the cmdlet returns.

Learn last week’s command: Save-Help.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.