I have the result of Get-ChildItem
, and I want to iterate over these, and display their names. By default if I simply use Write-Host
then I get it listed out along the row like this:
PerfLogs Program Files Program Files (x86) Python31 Temp Users Windows
However, say that I know I want it split into x columns, I want the output like this instead:
PerfLogs Python31 Windows
Program Files Temp
Program Files (x86) Users
As you can see, it lists it down the columns first, and then across.
Any idea how to get output like that? Ideally it would use the most # of columns as can fit on screen with the Name aligned to the left in each column.
UPDATE: thanks to Roman, I can now have my linux style 'ls' output with directory colors. Building off his updated script I have:
function color-ls
{
dir $args | Format-High -Print {
$item = $args
$fore = $host.UI.RawUI.ForegroundColor
$host.UI.RawUI.ForegroundColor = .{
if ($item[1].psIsContainer) {'Blue'}
elseif ($item[1].Extension -match '.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)') {'Red'}
elseif ($item[1].Extension -match '.(zip|tar|gz|rar)') {'Yellow'}
elseif ($item[1].Extension -match '.(py|pl|cs|rb|h|cpp)') {'Cyan'}
elseif ($item[1].Extension -match '.(txt|cfg|conf|ini|csv|log|xml)') {'Green'}
else {$fore}
}
write-host $args[0] -NoNewLine
$host.UI.RawUI.ForegroundColor = $fore
}
}
Output:
http://dl.dropbox.com/u/2809/lscolor.png
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…