1

I have a a similar question like this: Question 44460843

I want to use variables for the fields.

This example works well:

$x = ("Id,ProcessName,CPU").split(",")
Get-Process | ft -a $x

But how do i get it, if i want custom fields like this?

Get-Process | ft -a Id,ProcessName,@{Name = 'CPUx' ; Expression = {$_.CPU}}

I tried this, but it don't works:

$x = ("Id,ProcessName,@{Name = 'CPUx' ; Expression = {$_.CPU}}").split(",")
Get-Process | ft -a $x

Does anybody know how to do this? I am a lazy guy and want to avoid this copy/paste orgies with long text.

2
  • that looks like the splatting idea. take a look at Get-Help about_Splatting for how that works. [grin] Commented Mar 30, 2022 at 12:51
  • 2
    $x = 'Id','ProcessName',@{Name = 'CPUx' ; Expression = {$_.CPU}}; Get-Process | ft -a $x Commented Mar 30, 2022 at 13:08

1 Answer 1

2

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.
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.