Skip to content

Instantly share code, notes, and snippets.

@nbogie
Created December 12, 2025 00:07
Show Gist options
  • Select an option

  • Save nbogie/3222419e7270ea15c65d174454a802ff to your computer and use it in GitHub Desktop.

Select an option

Save nbogie/3222419e7270ea15c65d174454a802ff to your computer and use it in GitHub Desktop.
powershell script to list folders and their sizes - LLM generated - not reviewed
# Define the path to scan (use "." for the current directory)
$PathToScan = "."
Get-ChildItem -Path $PathToScan -Directory -ErrorAction SilentlyContinue |
ForEach-Object {
# Calculate the size of the current folder and its contents
$SizeInBytes = (Get-ChildItem -Path $_.FullName -Recurse -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum).Sum
# Convert bytes to Megabytes (MB)
# 1 MB = 1024 * 1024 bytes
$SizeInMB = $SizeInBytes / 1MB
# Create a custom object to hold the name and size
[PSCustomObject]@{
Folder = $_.Name
SizeMB = [Math]::Round($SizeInMB, 2)
}
} |
# Sort the results by SizeMB in descending order (largest first)
Sort-Object -Property SizeMB -Descending |
# Format the output table
Format-Table -AutoSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment