Lee_Dailey

function Get-DirTreeInfo

Feb 5th, 2019 (edited)
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-DirTreeInfo
  2.     {
  3.     <#
  4.     Comment Based Help goes here ...
  5.     #>
  6.  
  7.     [CmdletBinding ()]
  8.     Param (
  9.         [Parameter (
  10.             Position = 0
  11.             )]
  12.             [string[]]
  13.             $Path = $env:TEMP,
  14.  
  15.         [Parameter (
  16.             Position = 1
  17.             )]
  18.             [int]
  19.             $Depth = $Null
  20.         )
  21.  
  22.     begin {}
  23.  
  24.     process
  25.         {
  26.         foreach ($Dir in $Path)
  27.             {
  28.             # this is _ugly_
  29.             #    the default is 256 errors and that fills up with nearly identical errors _quickly_
  30.             #    is there another way detect an error while continuing the Get-ChildItem call?
  31.             $Error.Clear()
  32.  
  33.             $GCI_Params = @{
  34.                 LiteralPath = $Dir
  35.                 Recurse = $True
  36.                 Force = $True
  37.                 ErrorAction = 'SilentlyContinue'
  38.                 }
  39.             if ($Depth -gt 0)
  40.                 {
  41.                 $GCI_Params.Add('Depth', $Depth)
  42.                 }
  43.  
  44.             $DirTree = Get-ChildItem @GCI_Params
  45.             $DT_Stats = $DirTree.ForEach({$_.Length}) |
  46.                 Measure-Object -Sum -Maximum -Minimum
  47.             $DirCount = $DirTree.Where({$_.PSIsContainer}).Count
  48.  
  49.             [PSCustomObject]@{
  50.                 StartingPath = $Dir
  51.                 Depth = ('_Unlimited_', $Depth)[$Depth -gt 0]
  52.                 ErrorCount = $Error.Count
  53.                 FirstErrorText = $Error[0].Exception.Message
  54.                 ObjectCount = $DirTree.Count
  55.                 DirCount = $DirCount
  56.                 FileCount = $DirTree.Count - $DirCount
  57.                 TotalSize_MB = [math]::Round($DT_Stats.Sum / 1MB, 2)
  58.                 FileSize_Min_MB = [math]::Round($DT_Stats.Minimum / 1MB, 2)
  59.                 FileSize_Max_MB = [math]::Round($DT_Stats.Maximum / 1MB, 2)
  60.                 }
  61.             }
  62.         }
  63.  
  64.     end {}
  65.  
  66.     } # end >> function Get-DirTreeInfo
Add Comment
Please, Sign In to add comment