ConvertFrom-SourceTable

At first sight, all most every PowerShell developer would say that it is not purposed to restore an object list from a Format-Table output, but if you think out of the box, I would say: “why not?”.
The Format-Table output has a few quirks, but in general, a fixed-width table -which I would like to call aSourceTable– are most easy to read by human and can actually still be interpreted by a program. A very nice example is the following color table:

So, what is the catch?
In most cases the boundaries of the table columns can be determined from the header, ruler and data but in some cases it is undefinable because some table layouts, as Format-Table, might contain left- and right aligned columns and not enough data to determine the borders of the column:

Apart from this (which can be quiet easily resolved by replacing or providing a better ruler), there aren’t many other downsides to a SourceTable. Besides, if we consider centered column alignments as unsupported, an extra feature can taken in the SourceTable concept:

  • Every left aligned field will be considered as a string
  • Every right aligned field will be interpreted as an expression

In a lot of cases, values other than a string, e.g. numbers, are already right aligned by programs (and cmdlets like Format-Table) but unfortunately it is not (yet) done to an extend were even value types like arrays are right aligned and formatted for direct interpretation, like the RGB column in the color table example were the hexadecimal value column is parsed to an integer and the RGB column to an array. This feature gives an advantage to a CSV formatted table which only support strings and to a Xml formatted table which is very verbose and therefore hard to read.

SourceTable is intended for relative small tables and could be easily embedded together with the source code. In the future it might support multi-line fields and also have a PowerShell ConvertTo-SourceTable cmdlet to automatically create advanced source tables.

Cmdlet

The ConvertFrom-SourceTable cmdlet is available from the github iRon7/ConvertFrom-SourceTable repository and the central PowerShell Gallery repository:

Inspect

PS> Save-Script -Name ConvertFrom-SourceTable -Path <path>

Install

PS> Install-Script -Name ConvertFrom-SourceTable

Syntax

ConvertFrom-SourceTable [[-InputObject] <String[]>] [[-Header] <String>] [[-Ruler] <String>] [[-HorizontalRuler] <Char>] [[-VerticalRuler] <Char>] [-Literal] [-Markdown] [<CommonParameters>]

<String[]> | ConvertFrom-SourceTable [-Header <String>] [-Ruler <String>] [-HorizontalRuler <Char>] [-VerticalRuler <Char>] [-Literal] [-Markdown] [<CommonParameters>]

Examples

Parameters

InputObject <String[]>
Specifies the source table strings to be converted to objects. Enter a variable that contains the source table strings or type a command or expression that gets the source table strings. You can also pipe the source table strings to ConvertFrom-SourceTable.

*Note that piped table data strings are intermediately processed and released for the next cmdlet. In this mode, there is a higher possibility that floating data can’t be determined to be part of a specific column (as there is no overview of the table data that follows). To resolve this use the -Ruler parameter.

-Header
A string that defines the header line of an headless table. The headeris used to define the (property) names, the size and alignment of the column, therefore it is key that the columns names are properly alignedwith the rest of the column (including any table indents). The -Header parameter might also include the ruler functionality by omitting any ruler. In this case, all the horizontal ruler characters
will be replaced by spaces.

-Ruler
A string that replaces any ruler in the input table which helps to define character columns in occasions where the table column margins are indefinable.

-HorizontalRuler
Defines the horizontal ruler character. The default is a hyphen (-).

-VerticalRuler
Defines the vertical ruler character. The default is a vertical line (|).

-Literal
The -Literal parameter will prevent any right aligned data to be evaluated.

-Markdown
Threats the input table as a markdown table (-Markdown) or a source table (-Markdown:$False). By default, this parameter is automatically defined based on the existence of a vertical ruler character in the header.

Leave a Reply