1

Example class

Here's a simple class:

class ETF
{
    $Symbol
    [decimal]$Assets
}

Let's make a few instances:

$ls = (
    (New-Object ETF -Property @{ Symbol = 'ICLN'; Assets = 123456789 }),
    (New-Object ETF -Property @{ Symbol = 'FAN';  Assets = 234567890 }),
    (New-Object ETF -Property @{ Symbol = 'TAN';  Assets = 345678901 })
)

Format-Table

Display the objects in a table:

$ls | Format-Table
Symbol    Assets
------    ------
ICLN   123456789
FAN    234567890
TAN    345678901

Display Assets as currency

I'd like to display the Assets property as a currency value. Here's one way to do that:

$ls | Format-Table Symbol, @{ Label = 'Assets'; Expression = { '{0:C0}' -f $_.Assets } }
Symbol Assets      
------ ------      
ICLN   $123,456,789
FAN    $234,567,890
TAN    $345,678,901

format-etf-table

That's quite verbose for interactive use.

One way to deal with this is to define a function for displaying ETFs in a table:

function format-etf-table ()
{
    @($input) | Format-Table Symbol, @{ Label = 'Assets'; Expression = { '{0:C0}' -f $_.Assets } }
}
$ls | format-etf-table

The downside to a specialized function like format-etf-table is that we lose the flexibility of Format-Table. E.g. things like selecting which properties to display.

Types.ps1xml

I know that Types.ps1xml can be used to customize the display of data and can perhaps be used in a case like this.

However, this approach seems to require having the user add a ps1xml file to thier $PSHOME directory. This seems a little heavy handed.

Question

What's the recommended approach changing the default display of data in a table?

Do most people simply use ps1xml files?

Is there a way to achieve the effect of ps1xml files without installing them locally?

Is creating specialized formatting functions like format-etf-table what is most often done?

Are there other recommended approaches I haven't listed here?

dharmatech
  • 8,979
  • 8
  • 42
  • 88
  • 1
    In short: As of PowerShell 7.2.x, unfortunately, there is no _API_-based way to define custom formatting, so `.format.ps1xml` _files_ must be used - see the conceptual [about_Format.ps1xml](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Format.ps1xml) help topic (note that this is separate from custom _type_ modifications, as described in the conceptual [about_Types.ps1xml](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Types.ps1xml) help topic, which _do_ have APIs). – mklement0 Aug 17 '22 at 17:05
  • 1
    However, you can construct such files _on demand_ in a session - but that is nontrivial - a bare-bones example is in [this answer](https://stackoverflow.com/a/67526714/45375). – mklement0 Aug 17 '22 at 17:06
  • 1
    As an aside: I'd name your custom function `Format-EtfTable` - better not to use `-` twice in a command name. – mklement0 Aug 17 '22 at 17:11
  • 1
    As for what is most often done: For custom types emitted by commands that ship as part of _modules_, these modules typically come with their own `.format.ps1xml` files. Personally, I've rarely seen custom formatting functions. The inherent limitation of a function is that it must be applied _explicitly_ and, when you do, the output is no longer usable _as data_. – mklement0 Aug 17 '22 at 17:16

0 Answers0