Reputation: 67
I'd like the data presented by format-table to include the line or row number of the items. How can I achieve this?
some-command | ft
Line Something Else
---- --------- ----
0 Item1 Property1
1 Item2 Property2
Upvotes: 1
Views: 6329
Reputation: 27473
Here's my take. Some of my osx processes have no name as a regular user.
get-process | where name |
foreach { $line = 0 } { $line++; $_ } |
ft @{n='line';e={$line}}, name, id
line Name Id
---- ---- --
1 accountsd 368
2 AirPlayUIAgent 508
3 akd 2415
4 AMPDeviceDiscov 412
5 AppleSpell 1937
Take two:
ps | where name | select -first 5 |
foreach { $line = 0 } { $_ | add-member -passthru line (++$line) } |
select line,name,id
line Name Id
---- ---- --
1 accountsd 368
2 AirPlayUIAgent 508
3 akd 2415
4 AMPDeviceDiscov 412
5 AppleSpell 1937
Upvotes: 0
Reputation: 61103
You coiuld loop through the data and prepend an extra property Line
to the output using a counter variable.
Something like this:
# demo data
$data = [PsCustomObject]@{'Something' = 'Item1'; 'Else' = 'Property1'},
[PsCustomObject]@{'Something' = 'Item2'; 'Else' = 'Property2'},
[PsCustomObject]@{'Something' = 'Item3'; 'Else' = 'Property3'}
$count = 0
$data | ForEach-Object {
$_ | Select-Object @{Name = 'Line'; Expression = {$count}}, *
$count++
} | Format-Table -AutoSize
Output:
Line Something Else ---- --------- ---- 0 Item1 Property1 1 Item2 Property2 2 Item3 Property3
Upvotes: 3
Reputation: 9133
I believe you are looking something like this:
[ref]$id = 0 ; @( @{ youritems='a' }, @{ youritems='b' }, @{ youritems='c' }) |ForEach-Object{ new-object PSObject -Property $_ } | Format-Table @{ n="SerialNumber"; e={ "{0}" -f ++$id.value }; a="left" },youritems
The output would be :
SerialNumber youritems
------------- ---------
1 a
2 b
3 c
Upvotes: 0