Powershell: Autosize and Specific Column Width

前端 未结 3 1549
没有蜡笔的小新
没有蜡笔的小新 2020-12-20 02:18

Based on this SO question Powershell: Output collection of objects, I am able to wrap a collection of strings in multiple lines in one column. However, when I try to output

3条回答
  •  误落风尘
    2020-12-20 02:31

    I see what you mean, and I have no answer staying within the console, but if you do this to send it to Out-GridView:

    $col = @(
    
        (New-Object –TypeName PSObject –Prop @{'id'='01';'name'='a';'items'=@('the first item','the second item', 'the third item')}),
        (New-Object –TypeName PSObject –Prop @{'id'='02';'name'='b';'items'=@('the first item','the second item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item')}),
        (New-Object –TypeName PSObject –Prop @{'id'='03';'name'='c';'items'=@('the first item','the second item', 'the third item')})
        )
    
    $col|Select -p id,@{E={$_.items -join "`n"};L="Items"},name | Out-GridView
    

    (I trimmed the 'Expression' and 'Label=' so they fit on the screen).

    Then the GridView shows the columns with the right width.

    So if GridView can, why can't Format-Table? Maybe you can get around it with a custom view - http://www.adminarsenal.com/admin-arsenal-blog/bid/43912/PowerShell-Tips-for-System-Administrators-Format-Views, I haven't tried that approach.

提交回复
热议问题