Controlling column widths with Format-Table

前端 未结 2 547
情话喂你
情话喂你 2020-12-05 18:46

I am trying to get the last reboot time of some PCs from a list. When I use

foreach ($pc in $pclist) {
  Get-CimInstance -ClassName win32_operatingsystem -C         


        
2条回答
  •  没有蜡笔的小新
    2020-12-05 19:17

    If you want trim data past a certain length and manually specify column widths, you can pass a Width property for each property attribute.

    For example, if you wanted your data to look like this:

    Column 1    Column 2      Column 3      Column 4
    --------    --------      --------      --------
    Data        Lorem ip...   Lorem ip...   Important data
    

    Here's the basic Format-Table syntax, with a list of explicit properties:

    $data | Format-Table -Property Col1, Col2, Col3, Col4 -AutoSize
    

    Instead of just passing in the property names, we can add some additional metadata to Property:

    $a = @{Expression={$_.Col1}; Label="Column 1"; Width=30}, 
         @{Expression={$_.Col2}; Label="Column 2"; Width=30}, 
         @{Expression={$_.Col3}; Label="Column 3"; Width=30}, 
         @{Expression={$_.Col4}; Label="Column 4"; Width=30}
    $data | Format-Table -Property $a
    

    Note: I realize this is covered at the bottom of mklement0's more complete answer, but if this is your use case and you're scrolling quickly, I hope this helps highlight this strategy at a high level

提交回复
热议问题