How do I find the 10 largest files in a directory structure
This can be simplified a bit because Directories have no length:
gci . -r | sort Length -desc | select fullname -f 10
I've blogged about a similar problem where I want to find the largest files in a directory AND all subdirectories (the entire C: drive for example), as well as list their size in a nice easy to understand format (GB, MB, and KB). Here's the PowerShell function I use that lists all files sorted by size in a nice Out-GridView:
Get-ChildItem -Path 'C:\somefolder' -Recurse -Force -File |
Select-Object -Property FullName `
,@{Name='SizeGB';Expression={$_.Length / 1GB}} `
,@{Name='SizeMB';Expression={$_.Length / 1MB}} `
,@{Name='SizeKB';Expression={$_.Length / 1KB}} |
Sort-Object { $_.SizeKB } -Descending |
Out-GridView
Outputting to the PowerShell GridView is nice because it allows you to easily filter the results and scroll through them. This is the faster PowerShell v3 version, but the blog post also shows a slower PowerShell v2 compatible version.
And of course if you only want the top 10 largest files, you could just add a -First 10
parameter to the Select-Object call.
Try this script
Get-ChildItem -re -in * |
?{ -not $_.PSIsContainer } |
sort Length -descending |
select -first 10
Breakdown:
The filter block "?{ -not $_.PSIsContainer }
" is meant to filter out directories. The sort command will sort all of the remaining entries by size in descending order. The select clause will only allow the first 10 through so it will be the largest 10.