PowerShell 导入并排序 CSV 文件



我正在尝试导入CSV文件,然后按列排序。似乎无法使其工作。

$csvPath = 'path/to/usage_new.csv'
$usage = Get-Content -Path $csvPath | Select-Object -Skip 2 | Sort-Object "Percentage Used" | Out-String | ConvertFrom-Csv
foreach ($usage1 in $usage) {Write-Host Number $usage1.Phone used $usage1."Percentage Used"%}

这只是非常基本的,但排序对象不起作用?

它导入并打印到屏幕确定

任何建议都会很棒!

谢谢

Get-Content 将文本行读取为字符串,字符串没有称为"已用百分比"的属性,它们只有长度。

执行排序之前,您需要 CSV 进行转换,因此传入数据具有从列标题构建的"已用百分比"属性:

Import-Csv -Path $csvPath | 
    Select-Object -Skip 2 |
    Sort-Object -Property 'Percentage Used' |
    Select-Object Phone, @{Name='Used'; Expression={"{0}%" -f $_.'Percentage Used'}}

最新更新