我有Get-ChildItem
的结果,我想遍历它们,并显示它们的名称。如果我仅使用Write-Host
,则默认情况下会按行列出如下:
PerfLogs Program Files Program Files (x86) Python31 Temp Users Windows
然而,假设我知道我想将它分成x列,我希望输出的格式如下:
PerfLogs Python31 Windows
Program Files Temp
Program Files (x86) Users
如您所见,它首先按列列出,然后再横向排列。
有什么办法可以得到这样的输出吗?理想情况下,它应该使用尽可能多的列来适应屏幕,并在每个列中将名称靠左对齐。
更新:感谢Roman,我现在可以使用带有目录颜色的Linux风格“ls”输出。基于他更新后的脚本,我有:
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:
Format-High.ps1
文件放在哪里?我已经尝试将其放在C:\Users\X\Documents\WindowsPowerShell\Scripts\Format-High.ps1
目录下,也尝试将其与包含color-ls
的配置文件C:\Users\X\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
放在同一目录下,但是我仍然收到错误信息The term 'Format-High.ps1' is not recognized
。 - abulka