Lee_Dailey

function Get-SubDirSize

Feb 28th, 2020 (edited)
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-SubDirSize
  2.     {
  3.     [CmdletBinding ()]
  4.     Param (
  5.         [Parameter (
  6.             Position = 0
  7.             )]
  8.             [string]
  9.             $Path = $env:TEMP,
  10.  
  11.         [Parameter ()]
  12.             [switch]
  13.             $NoRecurse
  14.         )
  15.  
  16.     begin {}
  17.  
  18.     process
  19.         {
  20.         $DirList = Get-ChildItem -LiteralPath $Path -Directory -ErrorAction SilentlyContinue
  21.  
  22.         foreach ($DL_Item in $DirList)
  23.             {
  24.             $GCI_Params = @{
  25.                 LiteralPath = $DL_Item.FullName
  26.                 Recurse = -not $NoRecurse
  27.                 File = $True
  28.                 Force = $True
  29.                 ErrorAction = 'SilentlyContinue'
  30.                 }
  31.             $FileList = Get-ChildItem @GCI_Params
  32.             if ($FileList.Count -gt 0)
  33.                 {
  34.                 $DirSize = [math]::Round(($FileList |
  35.                     Measure-Object -Property Length -Sum).Sum / 1MB, 2)
  36.                 }
  37.                 else
  38.                 {
  39.                 $DirSize = 0
  40.                 }
  41.  
  42.             [PSCustomObject]@{
  43.                 DirName = $DL_Item.FullName
  44.                 FileCount = $FileList.Count
  45.                 Size_MB = $DirSize
  46.                 }
  47.             }
  48.         }
  49.  
  50.     end {}
  51.  
  52.     } # end >>> function Get-SubDirSize
Add Comment
Please, Sign In to add comment