How do I find the 10 largest files in a directory structure?
How do I find the 10 largest files in a directory structure?
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.
This can be simplified a bit because Directories have no length:
gci . -r | sort Length -desc | select fullname -f 10
Here is the fastest way to get this done I am aware of:
$folder = "$env:windir" # forder to be scanned
$minSize = 1MB
$stopwatch = [diagnostics.stopwatch]::StartNew()
$ErrorActionPreference = "silentlyContinue"
$list = New-Object 'Collections.ArrayList'
$files = &robocopy /l "$folder" /s \\localhost\C$\nul /bytes /njh /njs /np /nc /fp /ndl /min:$minSize
foreach($file in $files) {
$data = $file.split("`t")
$null = $list.add([tuple]::create([uint64]$data[3], $data[4]))
}
$list.sort() # sort by size ascending
$result = $list.GetRange($list.count-10,10)
$result | select item2, item1 | sort item1 -Descending
$stopwatch.Stop()
$t = $stopwatch.Elapsed.TotalSeconds
"done in $t sec."