Get-ChildItemSize.ps1


<#PSScriptInfo
 
.VERSION 1.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 d:\ -Multiple Megabyte -Recurse -Depth 1 -Directory | select -f 3 | ft -a
 
PSIsContainer Name BaseName Extension Length Size_Megabyte FullName Root PSDrive LinkType
------------- ---- -------- --------- ------ ------------- -------- ---- ------- --------
         True Clips Clips 20588609857 19634,828 D:\Clips D:\ D
         True HyperV HyperV 28415596228 27099,224 D:\HyperV D:\ D
         True null null 0 0 D:\null D:\ D
 
    
.EXAMPLE
  Get-ChildItemSize -Path d:\ -Multiple Gigabyte | select -f 3 | ft -a
 
PSIsContainer Name BaseName Extension Length Size_Gigabyte FullName Root PSDrive LinkType
------------- ---- -------- --------- ------ ------------- -------- ---- ------- --------
         True Clips Clips 20588609857 19,175 D:\Clips D:\ D
         True HyperV HyperV 28415596228 26,464 D:\HyperV D:\ D
         True null null 0 0 D:\null D:\ D
 
 
  
.EXAMPLE
  Get-ChildItemSize -Path d:\ -Multiple Gigabyte -Directory | select -f 3 | ft -a
 
PSIsContainer Name BaseName Extension Length Size_Gigabyte FullName Root PSDrive LinkType
------------- ---- -------- --------- ------ ------------- -------- ---- ------- --------
         True Clips Clips 20588609857 19,175 D:\Clips D:\ D
         True HyperV HyperV 28415596228 26,464 D:\HyperV D:\ D
         True null null 0 0 D:\null D:\ D
 
 
  
.EXAMPLE
  Get-ChildItemSize -Path d:\ -Multiple Megabyte -Recurse -Depth 1 -File | select -f 3 | ft -a
 
PSIsContainer Name BaseName Extension Length Size_Megabyte FullName Root PSDrive LinkType
------------- ---- -------- --------- ------ ------------- -------- ---- ------- --------
        False 13.jpg 13 .jpg 319686 0,305 D:\13.jpg D:\ D
        False 2 2 1649 0,002 D:\2 D:\ D
        False 2.mp4 2 .mp4 425241738 405,542 D:\2.mp4 D:\ D
 
  
#>
 

                param(
                [string[]]$Path = @(Get-Location),
                [switch]$Recurse = $False,
                [int]$Depth = 0,
                [ValidateSet('None','Kilobyte','Megabyte','Gigabyte','Terabyte','Petabyte')][string]$Multiple = 'Megabyte',
                [string[]]$Include,
                [string[]]$Exclude,
                [switch]$Directory = $False,
                [switch]$File = $False,
                [switch]$System = $False,
                [switch]$Hidden = $False,
                [switch]$Force = $False,
                [switch]$ReadOnly = $False,
                $Attributes,
                [switch]$UseTransaction = $False
                )
                
                @('Kilobyte','Megabyte','Gigabyte','Terabyte','Petabyte') | % -Begin {$MultipleINT = 1; $Multiples = [ordered]@{'None' = 1}} -Process {$Multiples.Add($_,($MultipleINT *= 1kb))}
                
                $ChildItem = @{
                               'Path' = $Path
                               'Recurse' = $Recurse
                               'Depth' = $Depth
                               'Include' = $Include
                               'Exclude' = $Exclude
                               'Directory' = $Directory
                               'File' = $File
                               'System' = $System
                               'Hidden' = $Hidden
                               'ReadOnly' = $ReadOnly
                               'UseTransaction' = $UseTransaction
                }
                
                $PSDrives = Get-PSDrive | Group-Object -Property Name -AsHashTable -AsString
                
                Get-ChildItem @ChildItem -ErrorAction SilentlyContinue | % {
                               $Size = [int64]0
                               $SizeMultiple = [int64]0
                               if ($_.PSIsContainer -eq $True) {
                                               $Size = [int64](Get-ChildItem -Path $_.FullName -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
                               } else {
                                               $Size = [int64]($_.Length)
                               }
                               
                               if($Size -gt 0){$SizeMultiple = [System.Math]::Round($($Size/($Multiples[[string]$Multiple])),3)}
                               
                                               [pscustomobject][ordered]@{
                                                               'PSIsContainer' = $_.PSIsContainer
                                                               'Name' = $_.Name
                                                               'BaseName' = $_.BaseName
                                                               'Extension' = $_.Extension
                                                               'Length' = $Size
                                                               $('Size_'+$Multiple) = $SizeMultiple
                                                               'FullName' = $_.FullName
                                                               'Root' = $PSDrives[[string]$_.PSDrive].Root
                                                               'PSDrive' = $_.PSDrive
                                                               'LinkType' = $_.LinkType
                                                               'LastWriteTime' = $_.LastWriteTime
                                                               'CreationTime' = $_.CreationTime
                                                               'DirectoryName' = $_.DirectoryName
                                                               'Mode' = $_.Mode
                                                               'Attributes' = $_.Attributes
                                               }
                }