powershell

Breaking up a input list, into several columns


How can I input a list, and have the contents display in multiple columns, say after three items; example:

$carlist = "c:\temp\list-of-cars.txt"             # listing 10 cars               
$num-cols = 4

Content would be:

Car1          
Car2
..       
Car9      
Car10

$c = $carlist.count/$num-cols                     # some number of columns  
write-host $carlist (something)

Output would be (based on the number of columns:

Car1          Car4          Car7          Car10                                             
Car2          Car5          Car8                           
Car3          Car6          Car9

Solution

  • $cars = get-content "list-of-cars.txt"
    $cars | Format-Wide {$_} -Column 4 -Force
    

    Or, if you prefer the one-liner:

    Get-Content .\list-of-cars.txt | Format-Wide {$_} -Column 4 -Force