Skip to content

Disk usage

PowerShell has no du, but Get-ChildItem plus Measure-Object does the job.

Terminal window
$bytes = (Get-ChildItem C:\Projects -Recurse -File -Force -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum).Sum
[math]::Round($bytes / 1GB, 2)

-ErrorAction SilentlyContinue hides the “Access denied” errors for folders you can’t read.

Terminal window
Get-ChildItem $HOME -Directory -Force -ErrorAction SilentlyContinue | ForEach-Object {
$size = (Get-ChildItem $_.FullName -Recurse -File -Force -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum).Sum
[pscustomobject]@{ Folder = $_.Name; SizeGB = [math]::Round($size / 1GB, 2) }
} | Sort-Object SizeGB -Descending | Select-Object -First 15

Run it again on the biggest folder to drill down level by level.

Terminal window
Get-ChildItem C:\ -Recurse -File -Force -ErrorAction SilentlyContinue |
Sort-Object Length -Descending |
Select-Object -First 20 FullName, @{ n = 'SizeMB'; e = { [math]::Round($_.Length / 1MB) } }

Files bigger than 500 MB under a folder:

Terminal window
Get-ChildItem D:\ -Recurse -File -ErrorAction SilentlyContinue |
Where-Object Length -gt 500MB |
Select-Object FullName, @{ n = 'SizeGB'; e = { [math]::Round($_.Length / 1GB, 2) } }

PowerShell understands KB, MB, GB and TB as numbers: 500MB is 524288000.

  • On a whole drive these commands are slow: they read the metadata of every file. For a visual view of a whole disk, a dedicated tool is faster.
  • Run PowerShell as administrator to measure system folders such as C:\Windows or other users’ profiles. Without it, those files are skipped silently.