Search notes:

PowerShell: Calculated Properties

Defining a calculated property

A calculated property is defined with a hash table.
Depending on the cmdLet where the calculated property is used, one or more of the following key-names are required or optional:
name or label Specifies the calculated property's name.
expression A script block to evaluate the calculated property's dynamic value.
alignment Possible values are 'left', 'right' or 'center', used in ConvertTo-HTML and Format-Table
formatstring
width Maximum width of a column, used in ConvertTo-HTML and Format-Table
depth
ascending and/or descending A boolean value that specifies the ordering.
Key names can be abbreviated, for example with e or expr for expression.

Example

0 .. 50 | foreach-object {
   [int] $x = 31 + [Math]::Sin($_/25*[Math]::Pi) * 30
   [psCustomObject] @{ x = $x}
} |
format-table @{
  expr = { (' ' *$_.x)+'*' }
  name = 'sine'
}

See also

Using calculated properties in select-object.

Index