Powershell: Output collection of objects -
i have collection of objects properties:
$col = @{....} and data this:
id name items --- ---- ----- 01 (a, b, c, d) 02 .... items array of strings. if output content of $col, data displayed above. there way output values this?
id name items --- ---- ----- 01 b c d 02 b 03 c c1 04 d d1 d2 05 ... updated, items column may contain empty, 1 or more 1 items.
first setup approximately object, test with:
$col = @( (new-object –typename psobject –prop @{'id'='01';'name'='a';'items'=@(1,2,3)}), (new-object –typename psobject –prop @{'id'='02';'name'='b';'items'=@(1,2,3)}), (new-object –typename psobject –prop @{'id'='03';'name'='c';'items'=@(1,2,3)}) ) then print it:
$col name id items ---- -- ----- 01 {1, 2, 3} b 02 {1, 2, 3} c 03 {1, 2, 3} then define custom format rule 'items' column, joins collection items using newline character:
$itemformat = @{expression={($_.items -join "`n")};label="items";width=6} then print collection format-table, using -wrap allow wrap long lines:
$col | format-table id,name,$itemformat -wrap -autosize which gives:
id name items -- ---- ------ 01 1 2 3 02 b 1 2 3 03 c 1 2 3
Comments
Post a Comment