Find the largest files and folders on a drive
List the biggest files and the biggest top-level folders under a path, so you can see what fills a drive without extra tools.
LanguagePowerShell
What it does to the systemReads only. It changes nothing.
A drive is full and you do not know why. On a server you often cannot install a disk usage tool.
This script walks a path one time. It lists the largest files and the size of each folder at the first level.
PowerShell
<#
Largest files and largest top-level folders under a path.
Run it as an administrator to read more folders. It only reads.
Example: .\Get-LargestItem.ps1 -Path D:\ -Top 15
#>
param(
[string]$Path = 'C:\',
# How many files and how many folders to show.
[int]$Top = 20
)
$root = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).ProviderPath.TrimEnd('\')
# Read every file one time. Folders you cannot open are skipped.
$files = @(Get-ChildItem -LiteralPath "$root\" -Recurse -File -Force -ErrorAction SilentlyContinue)
# Add each file to the total of its first-level folder.
$folderSize = @{}
foreach ($file in $files) {
$parts = $file.FullName.Substring($root.Length).TrimStart('\').Split('\')
if ($parts.Count -gt 1) { $folderSize[$parts[0]] += $file.Length }
}
$folderSize.GetEnumerator() |
Sort-Object Value -Descending |
Select-Object -First $Top |
ForEach-Object {
[pscustomobject]@{ Type = 'Folder'; SizeGB = [math]::Round($_.Value / 1GB, 2); Path = Join-Path "$root\" $_.Key }
}
$files |
Sort-Object Length -Descending |
Select-Object -First $Top |
ForEach-Object {
[pscustomobject]@{ Type = 'File'; SizeGB = [math]::Round($_.Length / 1GB, 2); Path = $_.FullName }
}
How to use it
- Save the script as
Get-LargestItem.ps1. - Open PowerShell as an administrator, so the script can read more folders.
- Run
.\Get-LargestItem.ps1 -Path D:\ -Top 15. - Run it again on the largest folder to go one level deeper.
- Add
| Format-Table -AutoSizeto see full paths.
Good to know
- A full system drive takes several minutes. Start with a folder if you already have a suspect.
- Windows PowerShell 5.1 follows junctions and links. A linked folder can count two times.
- Folders that even an administrator cannot open are left out, so the totals can be lower than the used space.
disk space
large files
folder size
Get-ChildItem
cleanup