Home > Software engineering >  Powershell how to use Get-command to display specific columns?
Powershell how to use Get-command to display specific columns?

Time:03-13

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.

CodePudding user response:

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.

CodePudding user response:

Try piping it to select object

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

  • Related