get-DiskUsgae.ps1

$dirRoot = "C:\"
 
function get-FolderSize($path){
  $total = 0
  $files = Get-ChildItem $path -ErrorAction SilentlyContinue
  foreach ($file in $files) {
    $total += $file.length
  }
  return $total
}
 
$results = @()
$dirs = get-ChildItem $dirRoot -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.mode -eq 'd----' }
 
foreach ($dir in $dirs) { 
  $childFiles = (get-ChildItem $dir.pspath -ErrorAction SilentlyContinue | Where-Object{ $_.mode -ne 'd----'})
  if (($childFiles).count) {$filecount = ($childFiles.count)}
  elseif ($childFiles)     {$filecount = 1                  }
  else                     {$filecount = 0                  }
 
  $childDirs = (get-ChildItem $dir.pspath -ErrorAction SilentlyContinue | Where-Object{ $_.mode -eq 'd----'})
  if (($childDirs).count) {$dircount = ($childDirs.count)}
  elseif ($childDirs)     {$dircount = 1                 }
  else                    {$dircount = 0                 }
     
  $result = New-Object psobject -Property @{Folder = (Split-Path $dir.pspath -NoQualifier); TotalSize = (get-FolderSize($dir.pspath)); FileCount = $filecount; SubDirs = $dircount}
  $results += $result
}
 
#$results | Select-Object Folder, TotalSize, FileCount, SubDirs | Sort-Object TotalSize -Descending | Format-Table -auto
$results | Select-Object Folder, TotalSize, FileCount, SubDirs | Sort-Object TotalSize -Descending | Out-GridView -Title "get-DiskUsage"
#Write-Host "Total of $($dirs.count) directories"