Functions/Get-FolderSize.ps1
function Get-FolderSize { [CmdletBinding()] param ( [Parameter()] [string] $Path = "." ) $TotalSize = 0 $Files = Get-ChildItem $Path -Recurse -File -Force $Files | ForEach-Object { # $_.Length $TotalSize += $_.Length } if ($TotalSize -gt 1024 * 1024 * 1024) { $TotalSize = $TotalSize / 1024 / 1024 / 1024 $UnitOfMeasurement = "GB" } elseif ($TotalSize -gt 1024 * 1024) { $TotalSize = $TotalSize / 1024 / 1024 $UnitOfMeasurement = "MB" } elseif ($TotalSize -gt 1024) { $TotalSize = $TotalSize / 1024 $UnitOfMeasurement = "kB" } else { $UnitOfMeasurement = "B" } $TotalSize = [math]::Round($TotalSize, 2) $TotalSize = "$TotalSize $UnitOfMeasurement" $Results = [PSCustomObject]@{ TotalSize = $TotalSize TotalItems = ($Files).Count } return $Results } |