Note:
- The answer applies analogously to the
Select-Object
cmdlet, which is what should be used to extract properties for later programmatic processing.
Format-*
cmdlets, such as Format-Table
in this case (whose built-in alias is ft
), are only intended to produce for-display formatting; see this answer for more information.
Theo has provided the solution in a comment:
$x = 'Id', 'ProcessName', @{ Name = 'CPUx' ; Expression = { $_.CPU } }
Get-Process | Format-Table -AutoSize $x # same as: ft -a $x
That is, the answer to the linked question applies in your case as well, even though it happens to use only literal strings as property names:
Directly construct an array of property names and calculated properties, using ,
, the array constructor operator, which allows you to use any mix of literals and variables; e.g.:
$pName = 'processName' # property name
$pCpu = @{ Name = 'CPUx' ; Expression = { $_.CPU } } # calculated property
$x = 'Id', $pName, $pCpu
Do not start with a single string that you split into an array of tokens with .Split()
, because it will limit you to property names, given that the tokens are invariably strings.
As an aside:
The array binds positionally to Format-Table
's (ft
's) -Property
parameter, and you can easily discover that as well as the fact that an array of property names / calculated properties is discovered as follows:
PS> Format-Table -?
...
SYNTAX
Format-Table [[-Property] <System.Object[]>] ...
DESCRIPTION
...
The outer [...]
tells you that the parameter is optional as a whole.
The [...]
around -Property
tells you that specifying the parameter name explicitly is optional, i.e. that positional binding is supported.
<System.Object[]>
tells you that an array ([]
) of System.Object
instances is expected as the type of the argument(s).
To get more information about what objects, specifically, may be passed, inspect the parameter individually:
PS> Get-Help Format-Table -Parameter Property
-Property <System.Object[]>
Specifies the object properties that appear in the display and the order
in which they appear. Type one or more property names, separated
by commas, or use a hash table to display a calculated property.
Wildcards are permitted.
...