Skip to content

Instantly share code, notes, and snippets.

@marshki
Created October 19, 2021 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marshki/0c45b564b201ac7e430823620cc84cab to your computer and use it in GitHub Desktop.
Save marshki/0c45b564b201ac7e430823620cc84cab to your computer and use it in GitHub Desktop.
PowerShell scipt to print sub-dirs of named dir--or present working dir (PWD)--sorted by size.
<#
.SYNOPSIS
Print sub-dirs of named dir--or present working dir (PWD)--sorted by size.
.Parameter Path
[sr-en] Specifies path of interest.
.Example
# Named directory.
PS> ./Get-DirSize.ps1 -Path "C:\USERS"
Path Time Size
---- ---- ----
curtislab 4/13/2021 8:20:44 AM 12.83 GB
dimartinolab 6/3/2021 3:34:29 PM 8.56 GB
phelpslab 10/12/2020 8:44:30 PM 7.84 GB
poeppellab 6/3/2021 1:44:52 PM 6.38 GB
castellanoslab 6/16/2021 11:19:57 AM 4.54 GB
hartleylab 10/12/2020 8:45:04 PM 2.92 GB
GlimcherLab 4/13/2021 3:59:45 PM 2.85 GB
freemanlab 10/10/2021 5:34:10 PM 1.94 GB
winawerlab 8/17/2021 12:33:45 PM 1.71 GB
miladlab 8/19/2021 10:30:01 AM 1.36 GB
rokerslab 10/21/2020 1:27:39 PM 1.18 GB
marmarlab 8/13/2021 8:18:05 AM 1.03 GB
.NOTES
Author: <mjk235 [at] nyu [dot] edu>
License: MIT
#>
$PrettySizeColumn = @{name="Size";expression={
$size = $_.Size
if ( $size -lt 1KB ) { $sizeOutput = "$("{0:N2}" -f $size) B" }
ElseIf ( $size -lt 1MB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1KB)) KB" }
ElseIf ( $size -lt 1GB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1MB)) MB" }
ElseIf ( $size -lt 1TB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1GB)) GB" }
ElseIf ( $size -lt 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1TB)) TB" }
ElseIf ( $size -ge 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1PB)) PB" }
$sizeOutput
}}
Get-ChildItem -Path $Path | Where-Object {$_.PSIsContainer} | ForEach-Object {
$size = ( Get-ChildItem -Path $_.FullName -Recurse | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum
$obj = new-object -TypeName psobject -Property @{
Path = $_.Name
Time = $_.LastWriteTime
Size = $size
}
$obj
} | Sort-Object -Property Size -Descending | Select-Object Path, Time, $PrettySizeColumn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment