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
<#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')
    $_
}