Created
July 18, 2012 19:22
-
-
Save gatesvp/3138244 to your computer and use it in GitHub Desktop.
PowerShell Get-FolderSizes
This file contains 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
### Sample usage: Get-FolderSizes "C:\data" | sort size -Descending | FT -AutoSize | |
function Get-FolderSizes([string] $StartFolder) { | |
$results = @(); | |
$colItems = (Get-ChildItem $StartFolder | Measure-Object -property length -sum) | |
$res = New-Object PSObject | select Folder, Size; | |
$res.Folder = $startFolder; | |
$res.Size = [float]("{0:N2}" -f ($colItems.sum / 1MB)); | |
$results += $res; | |
$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object) | |
foreach ($i in $colItems) | |
{ | |
$subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum -ErrorAction Ignore) | |
if($subFolderItems) | |
{ | |
$res = New-Object PSObject | select Folder, Size; | |
$res.Folder = $i.FullName; | |
$res.Size = [float]("{0:N2}" -f ($subFolderItems.sum / 1MB)) | |
$results += $res; | |
} | |
} | |
$results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment