Get-ChildItemSize.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

<#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
                                               }
                }