Disk usage
PowerShell has no du, but Get-ChildItem plus Measure-Object does the job.
Size of a folder
Section titled “Size of a folder”$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.
Biggest subfolders, sorted
Section titled “Biggest subfolders, sorted”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 15Run it again on the biggest folder to drill down level by level.
Biggest files
Section titled “Biggest files”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:
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.
Good to know
Section titled “Good to know”- 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:\Windowsor other users’ profiles. Without it, those files are skipped silently.
See also
Section titled “See also”- Free disk space
- The Linux version: Disk usage