1

I have a little problem while trying to add a row to a table. I stored the original values in a variable and used a custom object to transfer them into a table format.

$table = @( @{ColumnA="Able";    ColumnB=1; ColumnC=4},
            @{ColumnA="Baker";   ColumnB=2; ColumnC=3},
            @{ColumnA="Charlie"; ColumnB=3; ColumnC=5} )

$t2 = $table | ForEach {[PSCustomObject]$_} | Format-Table -AutoSize

After that I stored a new object in a variable and appent it to the table var.

$newRow = New-Object PsObject -Property @{ ColumnA = "James" ; ColumnB = 4 ; ColumnC = 12 }
$t2 += $newRow

$t2

My expectation was to get the following result:

ColumnC ColumnB ColumnA
------- ------- -------
      4       1 Able   
      3       2 Baker  
      5       3 Charlie
      12      4 James

But what I receive is this:

ColumnC ColumnB ColumnA
------- ------- -------
      4       1 Able   
      3       2 Baker  
      5       3 Charlie


ColumnC ColumnB ColumnA
------- ------- -------
     12       4 James  

How can I integrate all rows in ONE table?

thanks, Daniel

  • 1
    Format-Table is for display only. If you leave it as PsCustomObject you can add values then Format-Table once finished. – OwlsSleeping Jan 09 '21 at 00:24
  • 1
    `Format-*` cmdlets output objects whose sole purpose is to provide _formatting instructions_ to PowerShell's output-formatting system - see [this answer](https://stackoverflow.com/a/55174715/45375). In short: only ever use `Format-*` cmdlets to format data _for display_, never for subsequent _programmatic processing_. – mklement0 Jan 09 '21 at 01:42
  • 1
    Expanded my answer a bit to probably more of what you were expecting. – Glenn Jan 11 '21 at 15:54

2 Answers2

1

You've got an array of Hashtables. You need to append another Hashtable:

$table += @{ ColumnA = "James" ; ColumnB = 4 ; ColumnC = 12 }

$table

Name                           Value
----                           -----
ColumnA                        Able
ColumnC                        4
ColumnB                        1
ColumnA                        Baker
ColumnC                        3
ColumnB                        2
ColumnA                        Charlie
ColumnC                        5
ColumnB                        3
ColumnA                        James
ColumnC                        12
ColumnB                        4

There's no reason to use Format-Table, other than to change the display.

If you want to display them using the property names as header names you can do something like the following:

$table | %{ New-Object PSCustomObject -Property @{ ColumnA=$_.ColumnA; ColumnB=$_.ColumnB; ColumnC=$_.ColumnC } } | Select-Object -Property ColumnA, ColumnB, ColumnC

which will display them like this:

ColumnA ColumnB ColumnC
------- ------- -------
Able          1       4
Baker         2       3
Charlie       3       5
James         4      12
Glenn
  • 1,687
  • 15
  • 21
0

Actually, just removing Format-Table -AutoSize will fix your problem.

This,

$table = @( @{ColumnA="Able";    ColumnB=1; ColumnC=4},
            @{ColumnA="Baker";   ColumnB=2; ColumnC=3},
            @{ColumnA="Charlie"; ColumnB=3; ColumnC=5} )

$t2 = $table | ForEach {[PSCustomObject]$_} #| Format-Table -AutoSize
$newRow = New-Object PsObject -Property @{ ColumnA = "James" ; ColumnB = 4 ; ColumnC = 12 }
$t2 += $newRow
$t2

will output,

ColumnC ColumnB ColumnA
------- ------- -------
      4       1 Able
      3       2 Baker
      5       3 Charlie
     12       4 James
Christian Davén
  • 16,713
  • 12
  • 64
  • 77