Skip to content

Instantly share code, notes, and snippets.

@hpmmatuska
Last active August 29, 2015 14:14
Show Gist options
  • Save hpmmatuska/d881b60fadda9d0584ac to your computer and use it in GitHub Desktop.
Save hpmmatuska/d881b60fadda9d0584ac to your computer and use it in GitHub Desktop.
Get folder sizes with recourse. The output is list of first level directory structure under specified path.
Function mms-FolderSize {
<#
.Synopsis
The function will return folder size and list of first level subfolders size.
.Description
The function is simmilar to reskit command "diruse" or unix tool "du". The output is sum value of file sizes
in specified folder and summary siza for each first level subfolder. The function is build up on top of
get-childitem function.
.INPUTS
-Path <[System.IO.FileInfo]string> -ShowIn [B,MB,GB]
where paramter path:
- is default parameter, not mandatory.
- if present, function will run in the specified path (if exist)
- when paramater is missing, current folder is set
- an alias is "p"
parameter ShowIn:
B - the default parameter represent not formated output of get-child item in bytes (as Int)
MB - represent formated output (string) in MegaBytes, sorted decreasing
GB - represent formated output (string) in GigaBytes, sorted decreasing
.OUTPUTS
The default output is:
[String]Path
[Int]Size - when "ShowIn" param is missing, or
[String]Size - formated Int output with expression "{0:N2}"
.Example
List of current folder size and subfolder sizes:
PS C:\Temp> mms-FolderSize
Path Size
---- ----
C:\Temp 935494
C:\Temp\Microsoft Visual C++ 2010 x... 0
C:\Temp\Microsoft Visual C++ 2010 x... 0
C:\Temp\pulse 20924852
.Example
List of current folder size and subfolder sizes formated in MB
PS C:\Temp> mms-FolderSize -ShowIn MB
Path Size (M
B)
---- -------
C:\Temp\pulse 19,96
C:\Temp 0,89
C:\Temp\Microsoft Visual C++ 2010 x64 Redistributable Setup_10.0.40219 0,00
C:\Temp\Microsoft Visual C++ 2010 x86 Redistributable Setup_10.0.40219 0,00
.Example
List of specified path subfolders size, formated in MB
PS C:\> mms-FolderSize C:\RecoveryImage -ShowIn MB
PS C:\> mms-FolderSize -p C:\RecoveryImage -ShowIn MB
PS C:\> mms-FolderSize -path C:\RecoveryImage -ShowIn MB
Path Size (MB)
---- ---------
C:\RecoveryImage 2 816,05
C:\RecoveryImage\Drivers 672,57
C:\RecoveryImage\OEMInformation 0,21
.Example
The example of piped folder with manual expression and sorting
PS C:\>$env:systemroot | Get-FolderSize |
sort size -Descending |
select -First 10|
format-table Path, @{Name="Size (GB)";Expression={"{0:N2}" -f ($_.size / 1GB)}} -AutoSize
Path Size (MB)
---- ---------
C:\WINDOWS\WinSxS 6,11
C:\WINDOWS\System32 3,52
C:\WINDOWS\SysWOW64 1,20
C:\WINDOWS\assembly 0,85
C:\WINDOWS\Microsoft.NET 0,67
C:\WINDOWS\Fonts 0,59
C:\WINDOWS\Globalization 0,20
C:\WINDOWS\IME 0,16
C:\WINDOWS\Panther 0,14
C:\WINDOWS\Speech 0,12
.Notes
Last Updated: January 29, 2015
Version : 1.0
.Link
#>
[cmdletbinding(DefaultParameterSetName = "Path")]
Param(
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName, ParameterSetName="Path")]
[Alias("p")]
[String]$Path = $null,
[parameter()]
[ValidateSet('B','MB','GB')]
[String]$ShowIn
)
Function Get-Sizes {
$obj = new-object psobject
add-member -inp $obj noteproperty Path $path
$size = (Get-ChildItem $path -ErrorAction SilentlyContinue | Measure-Object -Sum Length -ErrorAction SilentlyContinue).Sum
if ($Size -gt 0){add-member -inp $obj noteproperty Size $size}
else {add-member -inp $obj noteproperty Size "0"}
$obj
get-childitem $path -ErrorAction SilentlyContinue | where {$_.PSIsContainer} | foreach {
$obj = new-object psobject
$size = (Get-ChildItem $_.FullName -recurse -ErrorAction SilentlyContinue| where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum
add-member -inp $obj noteproperty Path $_.fullName
if ($Size -gt 0){add-member -inp $obj noteproperty Size $size}
else {add-member -inp $obj noteproperty Size "0"}
$obj
} #foreach
}#function get-sizes
if (!$path){$path = Get-Location}
if (test-path $Path) {
If ($ShowIn.ToUpper() -eq 'MB'){get-sizes |sort size -Descending |ft Path, @{Name="Size (MB)";Expression={"{0:N2}" -f ($_.size / 1MB)}} -AutoSize}
elseIf ($ShowIn.ToUpper() -eq 'GB'){get-sizes |sort size -Descending |ft Path, @{Name="Size (GB)";Expression={"{0:N2}" -f ($_.size / 1GB)}} -AutoSize}
else {Get-Sizes}
} else {Write-Output ('Path "'+$Path+'" Does not exist')}
} #function mms-foldersize
#mms-FolderSize c:\users #|sort size -Descending |ft Path, @{Name="Size (MB)";Expression={"{0:N2}" -f ($_.size / 1MB)}} -AutoSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment