Find what is taking up space by traversing your directories from any starting point you specify.
You will then be given an ordered listing of what directories are taking up the most space.
This helped me find that docker had created over 300 GB of log files in a normally hidden folder.
Neither windows storage nor Microsoft's PC Manager was able to identify this for me. So I wrote this script:
# Check if the script is running as Administrator
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
# Restart script with elevated privileges
Start-Process powershell "-File `"$PSCommandPath`"" -Verb RunAs
exit
}
# Function to calculate folder sizes
function Get-FolderSizes {
param (
[string]$Path = "C:\", # Default path, change to a specific folder if desired
[bool]$DeepScan = $false # Set to $true for full recursive scan
)
# Determine the recursion depth based on the DeepScan switch
$depth = if ($DeepScan) { [int]::MaxValue } else { 1 }
# Get all subfolders up to the specified depth
$folders = Get-ChildItem -Path $Path -Directory -Recurse -Depth $depth -Force -ErrorAction SilentlyContinue
# Calculate sizes and store results in an array
$folderSizes = @()
foreach ($folder in $folders) {
# Output the current folder being processed
Write-Output "Scanning folder: $($folder.FullName)"
try {
# Calculate size by summing file lengths within each folder
$size = (Get-ChildItem -Path $folder.FullName -Recurse -File -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
$folderSizes += [PSCustomObject]@{
Folder = $folder.FullName
SizeMB = [math]::round($size / 1MB, 2) # Convert size to MB and round to 2 decimal places
}
}
catch {
Write-Output "Skipping folder due to access restrictions: $($folder.FullName)"
}
}
# Sort by size in descending order and return
$folderSizes | Sort-Object SizeMB -Descending
}
# Prompt the user for the directory to analyze
$targetPath = Read-Host "Enter the path of the drive or folder you want to analyze (e.g., C:\)"
# Prompt the user if they want a full recursive scan
$deepScanChoice = Read-Host "Do you want to scan all subdirectories fully (y/n)?"
$deepScan = if ($deepScanChoice -match "^(y|Y)$") { $true } else { $false }
# Run the function and display results
$results = Get-FolderSizes -Path $targetPath -DeepScan $deepScan
$results | Format-Table -AutoSize