Functions/Get-FolderSize.ps1
<#
.SYNOPSIS Shows size of a folder and optionally of all it's subfolders .DESCRIPTION Calculates the size of all items contained in a filesystem directory. Optionally you can show the size of each subdirectory. .PARAMETER Path Path to root directory .PARAMETER ShowSubItems Show size of subitems .EXAMPLE Get-FolderSize -Path "C:\Users" -ShowSubItems $true Description ----------- Shows size of C:\Users and each folder contained directly in C:\Users #> function Get-FolderSize { [CmdletBinding()] [OutputType([String])] Param ( [Parameter( Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)] [String]$Path, [Switch][Bool]$ShowSubItems ) Begin { } Process { if ($ShowSubItems) { $colItems = (Get-ChildItem -Force -Path $Path | Measure-Object -property length -sum) Write-Output -InputObject ("$Path -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB") $colItems = (Get-ChildItem -Force -Path $Path | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object) foreach ($i in $colItems) { $subFolderItems = (Get-ChildItem -Force -Recurse -Path $i.FullName | Measure-Object -property length -sum) Write-Output -InputObject ($i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB") } } else { $colItems = (Get-ChildItem -Force -Recurse -Path $Path | Measure-Object -property Length -sum) Write-Output -InputObject ("$Path -- " + "{0:N3}" -f ($colItems.sum / 1GB) + " GB") } } End { } } |