← script library

Directory Size Report Script

Files & FoldersJanuary 11, 2025

In this post, we will share a PowerShell script that generates a report of the sizes of all subdirectories within a specified directory. This is useful for assessing space usage and managing storage effectively.

powershell
$targetDirectory = 'C:\Path\To\Your\Directory'

$subDirectories = Get-ChildItem -Path $targetDirectory -Directory

$directorySizes = @{}
foreach ($subDir in $subDirectories) {
    $size = (Get-ChildItem -Path $subDir.FullName -Recurse | Measure-Object -Property Length -Sum).Sum
    $directorySizes[$subDir.FullName] = [math]::round($size / 1MB, 2) # Convert to MB
}

Write-Host "Directory Size Report:"
foreach ($key in $directorySizes.Keys) {
    Write-Host "$key - $($directorySizes[$key]) MB"
}

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.