0

I’m using a hashtable for custom formatting of Format-Table output.

The table input is a System.IO.FileInfo array, and I need columns showing file name, last write time, and file size. My code so far:

$queue = Find-FilesToUpload -server $server | Sort-Object -property lastWriteTime | `
   Format-Table -property name, lastWriteTime, 
   @{name = "Bytes"; expression = {$_.length}; alignment="right"; width=15; format="n0"} | `
   Out-Host -paging

The output looks like this:

Name         LastWriteTime          Bytes
----         -------------          -----
pierson.ogg  8/23/2023 2:06:08 PM 460,664
ohiamsoh.htm 8/23/2023 2:06:55 PM   3,501

The columns are so close together that they can be hard to read with large tables. So I’m trying to put more space between columns, something like this:

Name            LastWriteTime             Bytes
----            -------------             -----
pierson.ogg     8/23/2023 2:06:08 PM    460,664
ohiamsoh.htm    8/23/2023 2:06:55 PM      3,501

Is adding more inter-column space possible with Format-Table? Neither the -AutoSize parameter or “width” argument made any difference.

I also tried the field width component that I would normally use with the string -f format operator, but couldn’t find a syntax that worked.

aksarben
  • 588
  • 3
  • 7
  • 1
    this is a bug with `format-table`, something very similar happens with a custom format file, you cant have the first 2 columns with a dynamic width and the last column with a fixed width. you would need to use fixed width for the first 2 columns too, i.e.: `format-table @{ n = 'name'; e = 'name'; w = 20 }, @{ n = 'lastWriteTime'; e = 'lastWriteTime'; w = 25 }, @{ n = 'Bytes'; e = { $_.length }; a = 'right'; w = 15; f = 'n0' }` – Santiago Squarzon Aug 24 '23 at 00:49

1 Answers1

0

After monkeying with this question for more time than I care to admit, I finally concluded I was over-engineering it. I decided to switch from Format-Table to Write-Host, and came up with this:

$queue = Find-FilesToUpload -server $server | Sort-Object -property lastWriteTime
Write-Host ("`n{0,-18} {1} {2,15}" -f "Name", "Last Written", "Size")
Write-Host "-----------------------------------------------"

foreach ($file in $queue) {
   Write-Host ("{0,-15} {1} {2,10:n0}" -f $file.name, $file.lastWriteTime, $file.length)
}

which gives this output:

Name               Last Written            Size
-----------------------------------------------
plymouth.ogg    8/23/2023 6:10:42 PM    248,551
ohighesj.htm    8/23/2023 6:11:57 PM      3,511
aksarben
  • 588
  • 3
  • 7