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?