Get-ChildItemSize.ps1

<#PSScriptInfo
.VERSION 1.2.0
.GUID ae037294-3b3a-44ea-a690-b4d218dee2c7
.AUTHOR saw-friendship
.COMPANYNAME
.COPYRIGHT
.TAGS FileSystemObject Measure Size Recurse
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
 
#>


<#
  
.DESCRIPTION
 Measure size file system objects. Use Multiple param for convert size in byte to Kilobyte,Megabyte,Gigabyte,Terabyte,Petabyte
   
.EXAMPLE
Get-ChildItemSize -Path C:\Windows -Multiple Megabyte -Recurse -Depth 1 -Directory | select -f 3 -Skip 5
 
Mode Name Length Size FullName LastWriteTime CreationTime
---- ---- ------ ---- -------- ------------- ------------
d-r--- assembly 4201863612 4007.209 C:\Windows\assembly 27.08.2019 21:01:49 19.03.2019 7:52:43
d----- bcastdvr 785153 0.749 C:\Windows\bcastdvr 22.08.2019 11:30:41 19.03.2019 7:52:44
d----- Boot 39549067 37.717 C:\Windows\Boot 19.03.2019 7:52:44 19.03.2019 7:52:44
     
.EXAMPLE
Get-ChildItemSize -Path C:\Windows -Multiple Gigabyte -ToString | select -f 3 -Skip 5
 
Mode Name Length Size FullName LastWriteTime CreationTime
---- ---- ------ ---- -------- ------------- ------------
d-r--- assembly 4201863612 3.913GB C:\Windows\assembly 27.08.2019 21:01:49 19.03.2019 7:52:43
d----- bcastdvr 785153 0.001GB C:\Windows\bcastdvr 22.08.2019 11:30:41 19.03.2019 7:52:44
d----- Boot 39549067 0.037GB C:\Windows\Boot 19.03.2019 7:52:44 19.03.2019 7:52:44
  
   
.EXAMPLE
Get-ChildItemSize -Path C:\Windows -Multiple Gigabyte -Directory | select -f 3 -Skip 5
 
Mode Name Length Size FullName LastWriteTime CreationTime
---- ---- ------ ---- -------- ------------- ------------
d-r--- assembly 4201863612 3.913 C:\Windows\assembly 27.08.2019 21:01:49 19.03.2019 7:52:43
d----- bcastdvr 785153 0.001 C:\Windows\bcastdvr 22.08.2019 11:30:41 19.03.2019 7:52:44
d----- Boot 39549067 0.037 C:\Windows\Boot 19.03.2019 7:52:44 19.03.2019 7:52:44
 
   
.EXAMPLE
Get-ChildItemSize -Path C:\Windows -Multiple Megabyte -Recurse -Depth 1 -File | select -f 3 -Skip 5
 
Mode Name Length Size FullName LastWriteTime CreationTime
---- ---- ------ ---- -------- ------------- ------------
-a---- DfsrAdmin.exe 232960 0.222 C:\Windows\DfsrAdmin.exe 26.08.2019 20:18:25 26.08.2019 20:18:25
-a---- DfsrAdmin.exe.c... 1315 0.001 C:\Windows\DfsrAdmin.exe.config 26.08.2019 20:18:33 26.08.2019 20:18:34
-a---- diagerr.xml 26673 0.025 C:\Windows\diagerr.xml 31.05.2019 17:03:40 31.05.2019 17:03:23
 
#>
 

param(
    [string[]]$Path = @(Get-Location),
    [switch]$Recurse = $False,
    [int]$Depth = 0,
    [ValidateSet('None','Kilobyte','Megabyte','Gigabyte','Terabyte','Petabyte')][string]$Multiple = 'Megabyte',
    [switch]$ToString,
    [int]$Round = 2,
    [string[]]$Include,
    [string[]]$Exclude,
    [switch]$Directory = $False,
    [switch]$File = $False,
    [switch]$System = $False,
    [switch]$Hidden = $False,
    [switch]$Force = $False,
    [switch]$ReadOnly = $False,
    [string[]]$Attributes,
    [switch]$UseTransaction
)
    
$Multiples = @{
    None = @{Alias = 'B'; Value = 1}
    Kilobyte = @{Alias = 'KB'; Value = 1KB}
    Megabyte = @{Alias = 'MB'; Value = 1MB}
    Gigabyte = @{Alias = 'GB'; Value = 1GB}
    Terabyte = @{Alias = 'TB'; Value = 1TB}
    Petabyte = @{Alias = 'PB'; Value = 1PB}
}

$ChildItem = @{
   Path = $Path
   Recurse = $Recurse
   Depth = $Depth
   Include = $Include
   Exclude = $Exclude
   Directory = $Directory
   File = $File
   Attributes = $Attributes
   System = $System
   Hidden = $Hidden
   ReadOnly = $ReadOnly
   UseTransaction = $UseTransaction
}

$PSDrives = Get-PSDrive | Group-Object -Property Name -AsHashTable -AsString

Get-ChildItem @ChildItem -ErrorAction SilentlyContinue | Select-Object -Property @(
    ,'*'
    ,@{Name = 'Measure'; Expression = {
        if ($_.PSIsContainer) {
            Get-ChildItem -Path $_.FullName -Recurse -Force | Measure-Object -Property Length -Sum
        } else {
            $_ | Measure-Object -Property Length -Sum
        }
    }}
) | Select-Object -Property @(
    ,'PSIsContainer'
    ,'Name'
    ,'BaseName'
    ,'Extension'
    ,@{Name = 'ObjectCount'; Expression = {$_.Measure.Count}}
    ,@{Name = 'Length'; Expression = {$_.Measure.Sum}}
    ,@{Name = 'Size'; Expression = {
        if ($ToString) {
            [string]([System.Math]::Round($($_.Measure.Sum/($Multiples[[string]$Multiple]['Value'])),$Round)) + $Multiples[$Multiple]['Alias']
        } else {
            [System.Math]::Round($($_.Measure.Sum/($Multiples[[string]$Multiple]['Value'])),$Round)
        }
    }}
    ,@{Name = 'Root'; Expression = {$PSDrives[[string]$_.PSDrive].Root}}
    ,'FullName'
    ,'PSDrive'
    ,'LinkType'
    ,'LastWriteTime'
    ,'CreationTime'
    ,'Mode'
    ,'Attributes'
) | % {
    $_.psobject.TypeNames.Insert(0,'FS.ChildItemSize')
    $_
}