Last active
April 9, 2021 13:41
-
-
Save bmatthewshea/1ac4edaac851f03f97444973dadc9028 to your computer and use it in GitHub Desktop.
Show data used by top level folders using fast FSO approach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Default Path can be an argument - otherwise it defaults to CWD/Get-Location | |
# Script breaks on long filenames/path and "special" folders like C:\Users\User\Documents - needs work. | |
# Examples: | |
# ./show-folders-fast | |
# ./show-folders-fast "C:\Windows" | |
# | |
param([String]$folder = "$(Get-Location)") | |
$output_hash_table = @{} | |
$folder_count = (Get-ChildItem -Directory -EA 0 $folder | Measure-Object).count | |
$i=0 | |
$size = 0 | |
$fso = New-Object -ComObject Scripting.FileSystemObject | |
ForEach($dir in (Get-ChildItem $folder -Directory)) | |
{ | |
$directory = $fso.getfolder($dir.FullName) | |
$size += $directory.size | |
$output_hash_table.add( $dir.name, $directory.size ) | |
$i++ | |
Write-Progress -Activity "Indexing Folders.." -Status "Scanned $i of $folder_count folders" -PercentComplete (($i / $folder_count) * 100); | |
} | |
Write-Output "`nCompleted. Sorting low to high.." | |
# Creates new sorted (ordered) hash table - PS3.0+ only: | |
$output_hash_sorted = [ordered] @{} | |
$output_hash_table.GetEnumerator() | Sort Value | foreach {$output_hash_sorted[$_.Key] = $_.Value} | |
Write-Output "`n$($folder_count) results for $($folder)`n" | |
# Use GetEnumerator to sort by key or value - use Value (size) - format it all as table | |
# $output_hash.GetEnumerator() | Sort Value | Format-Table | |
Write-Output "Folder Size in Bytes Size in Megabytes" | |
Write-Output "================================ =============== =================" | |
$size_in_bytes = 0 | |
ForEach($hashkey in $output_hash_sorted.Keys) | |
{ | |
if (([string]$output_hash_sorted[$hashkey]).Trim()) { | |
$size_in_bytes = $output_hash_sorted[$hashkey] | |
} | |
$size_in_mb = $size_in_bytes/1MB | |
$hashkey = $hashkey[0..31] -join "" # shorten any folder name to 32 chars | |
$PrintHashLine = "{0,32:f10}{1,21}{2,21:n2}" -f $hashkey, $size_in_bytes, $size_in_mb | |
Write-Output $PrintHashLine | |
} | |
$FinalTally="Total Size in Bytes : {0}`nTotal Size in MBytes: {1:n2}`nTotal Size in GBytes: {2:n2}" -f $size, ($size/1MB), ($size/1GB) | |
Write-Output "`n$($FinalTally)`n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment