我查看了各種建議Sort-Object將回傳陣列的站點。這不是我在執行以下操作時看到的,由 引起的最終輸出Sort-Object只是一個字串。如何將陣列排序到另一個陣列而不是字串?
$myprogs = Get-ChildItem D:\MyPath\MyPrograms
$myprognames = foreach ($i in $myprogs) { $i.FullName -replace "D:\\MyPath\\MyPrograms\\", "" }
# At this point, $myprognames is a normal array
$myprognames = $myprognames | Sort-Object -Unique
$myprognames
# At this point, $myprognames is a single string with all elements stuck together, and if I select an element, like $myprognames[17], I will just get a single character as it's a string.
uj5u.com熱心網友回復:
如果我假設正確,您想要類似的東西:
$myprogs =
Get-ChildItem 'D:\MyPath\MyPrograms' |
Select-Object -Property *,
@{Name = 'ShortenedPath'; Expression = {$_.FullName -replace ([regex]::escape('D:\MyPath\MyPrograms'))}}
$MyProgNames = $myprogs |
Sort-Object -Property ShortenedPath -Unique
當然,您應該列出您實際使用的所有屬性,而不是使用-Property *
根據您的評論,您實際上是在查找已排序檔案夾的名稱,對嗎?如果是這種情況,那么您就過于復雜了。;-)
$myprogs =
Get-ChildItem 'D:\MyPath\MyPrograms' |
Select-Object -Property Name |
Sort-Object -Property Name -Unique
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/525604.html
標籤:数组电源外壳排序排序对象
