0

I need to use 'Get-Command" to display three specific columns of information. Name, CommandType, and Module. I am new to PowerShell and have been searching for hours to find an answer but have not been able to find any documentation that explains how to do this specifically.

  • Note: The post previously believed to be a duplicate, [How to get an object's property's value by property name?](https://stackoverflow.com/questions/14406315/how-to-get-an-objects-propertys-value-by-property-name), solves a _different_ problem, namely how extract a _single property value_. – mklement0 Mar 12 '22 at 18:25

2 Answers2

2

I need to use 'Get-Command" to display three specific columns of information. Name, CommandType, and Module

Since your intent is to display the columns of interest, in tabular format, you can use Format-Table:

Get-Command | Format-Table -Property Name, CommandType, Module

For quick interactive use, you can shorten the command, by using aliases and positional parameter binding:

gcm | ft name, commandtype, module

However, note that Format-* cmdlets are only suited to for-display formatting, not for outputting data suitable for later programmatic processing - see this answer and the Select-Object solution below.


By contrast, if you need to construct new objects that have only a subset of the properties of the input objects (while possibly adding new properties / transforming or renaming existing ones via calculated properties[1]), you can use the Select-Object cmdlet:

Get-Command | Select-Object -Property Name, CommandType, Module

Note: If you print this command's output to the console (host), it will result in the same display output as the Format-Table call above, because PowerShell implicitly uses Format-Table formatting for objects that have 4 or fewer (public) properties.[2]

However, for explicit control of display formatting - such as if you want 5-property objects to be shown in tabular format too - Format-* cmdlets must be used.


[1] You may also use calculated properties with Format-Table, among other cmdlets.

[2] 5 or more properties result in Format-List formatting; Generally, this implicit formatting applies only to .NET types that do not have predefined formatting data associated with them; if they do, that data controls the default view and other aspects - see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
-1

Try piping it to select object

get-command|select-object -expandpoperty Name, CommandType, and Module

Nawad-sama
  • 151
  • 2
  • 12