Reputation: 12794
Using PowerShell, how do I sort all files in all folders by size? Whenever I use the results of Get-ChildItem, even after it's saved to a variable and modified, it groups all files by directory. Even then, the files aren't sorted in each directory.
$files = Get-ChildItem -File -Filter *.bat -Path C:\git -Recurse
Sort-Object -InputObject $files -Property Length
Upvotes: 5
Views: 11365
Reputation: 891
Don't have the Reputation to comment yet (so I hope this is ok with stackoverflows etiquette), but Bacon Bits answer could be improved a bit by switching Select-Object and Sort-Object around:
Get-ChildItem -File -Filter *.bat -Path C:\git -Recurse | Select-Object -Property Length, Name | Sort-Object -Property Length | Format-Table -AutoSize
This will improve performance especially on large filesystems (about 50% if I use the above code on my C-drive)
Upvotes: 3
Reputation: 32145
I'm guessing you're running into the default display mode of System.IO.FileInfo
, which displays empty lines and the directory name whenever it changes. It also takes forever to display because the Mode
column is particularly poorly optimized.
Try something like this:
Get-ChildItem -File -Filter *.bat -Path C:\git -Recurse |
Sort-Object -Property Length |
Select-Object -Property Length, Name |
Format-Table -AutoSize
Upvotes: 9