如何输入列表,并将内容显示在多个列中,比如三个项目之后;例如:
$carlist = "c:\temp\list-of-cars.txt" # listing 10 cars
$num-cols = 4
内容将是:
Car1
Car2
..
Car9
Car10
$c = $carlist.count/$num-cols # some number of columns
write-host $carlist (something)
输出将是(基于列数:
Car1 Car4 Car7 Car10
Car2 Car5 Car8
Car3 Car6 Car9
答案 0 :(得分:2)
尝试Format-Wide
cmdlet:
Get-Content cars.txt | Select-Object @{n='name';e={$_}} | Format-Wide -Column 4
答案 1 :(得分:1)
$cars = get-content "list-of-cars.txt"
$cars | Format-Wide {$_} -Column 4 -Force
或者,如果您更喜欢单行:
Get-Content .\list-of-cars.txt | Format-Wide {$_} -Column 4 -Force
答案 2 :(得分:0)
这是一种方式:
Function SplitTo-Column($colnum, [object[]]$list)
{
for($i=1; $i -le $list.Length; $i++)
{
if ( ($i % $colnum) -eq 0)
{
[Console]::WriteLine($list[$i-1]) #or write-host "$($list[$i-1])" is more posh
}
elseif ($i -eq $list.count)
{
[Console]::WriteLine($list[$i-1]) #or write-host "$($list[$i-1])"
}
else
{
[Console]::Write($list[$i-1] + "`t" ) #or write-host "$($list[$i-1])`t" -NoNewline
}
}
}
使用:
$carlist = gc c:\temp\list-of-cars.txt # listing 10 cars
$numcols = 4
SplitTo-Column -colnum $numcols -list $carlist