PoshCode Archive  Artifact [069436ff5d]

Artifact 069436ff5ddab823402bcadb0fad11bfbbaf57181d89b1133bdda621aba447f9:

  • File Get-DiskUsage.ps1 — part of check-in [640f543635] at 2018-06-10 14:23:01 on branch trunk — The Linux/Unix ‘du -sh’ command, ala PowerShell (faster than other versions of this script, but still ever so much slower than compiled code) (user: Joel Bennett size: 1801)

# encoding: ascii
# api: powershell
# title: Get-DiskUsage
# description: The Linux/Unix ‘du -sh’ command, ala PowerShell (faster than other versions of this script, but still ever so much slower than compiled code)
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# x-poshcode-id: 721
# x-archived: 2017-01-04T03:15:10
# x-published: 2009-12-08T17:26:00
#
#
## du.ps1 Get-DiskUsage
###############################
## Note: $unit can be: kb, mb, gb, or an empty string (to get bytes)
###############################
## -Force causes du to include System ReparsePoints 
##   This means including folders like Vista's ~\Documents\My Pictures (which is a symlink to ~\Pictures)
## -Total forces a final "total" sum row which represents the total for $Path
##
function du {
PARAM($path = "$pwd", [switch]$force, $unit="MB", $round=2, [switch]$total) 
   &{ 
   foreach($dir in (get-childitem $path -force | 
         Where { $_.PSIsContainer -and ($Force -or ($_.Attributes -notmatch "ReparsePoint" -or
                                                    $_.Attributes -notmatch "System"))})) 
   { 
      get-childitem -lit $dir -recurse -force | measure-object -sum -prop Length -EA "SilentlyContinue"  |
         Select-Object @{Name="Path"; Expr={$dir}}, Count, Sum
   } } | Tee-Object -Variable Totals | 
       Select-Object Path, Count, @{Name="Size($unit)"; Expr={[Math]::Round($_.Sum/"1$unit",$round)}} 
   if($total) {
      $totals = $totals | measure-object -Sum -Prop "Count","Sum"
      Get-Item $path | Select @{Name="Path"; Expr={$_}}, 
                              @{Name="Count"; Expr={$totals[0].Sum}}, 
                              @{Name="Size($unit)"; Expr={[Math]::Round((($totals[1].Sum)/"1$unit"),$round)}}
   }
}