forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-FolderSizeInfo.ps1
More file actions
182 lines (160 loc) · 7.12 KB
/
Get-FolderSizeInfo.ps1
File metadata and controls
182 lines (160 loc) · 7.12 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
Function Get-FolderSizeInfo {
[cmdletbinding()]
[alias("gsi")]
[OutputType("FolderSizeInfo")]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter a file system path like C:\Scripts.", ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[alias("PSPath")]
[string[]]$Path,
[Parameter(HelpMessage = "Include hidden directories")]
[switch]$Hidden
)
Begin {
Write-Verbose "Starting $($MyInvocation.MyCommand)"
} #Begin
Process {
foreach ($item in $path) {
$cPath = (Convert-Path -literalpath $item)
Write-Verbose "Measuring $cPath on $([System.Environment]::MachineName)"
if (Test-Path -literalpath $cPath) {
$d = [System.IO.DirectoryInfo]::new($cPath)
$files = [system.collections.arraylist]::new()
If ($psversiontable.psversion.major -gt 5 ) {
#this .NET class is not available in Windows PowerShell 5.1
$opt = [System.IO.EnumerationOptions]::new()
$opt.RecurseSubdirectories = $True
if ($hidden) {
Write-Verbose "Including hidden files"
$opt.AttributesToSkip = "SparseFile", "ReparsePoint"
}
else {
$opt.attributestoSkip = "Hidden"
}
$data = $($d.GetFiles("*", $opt))
if ($data.count -gt 1) {
$files.AddRange($data)
}
elseif ($data.count -eq 1) {
[void]($files.Add($data))
}
} #if newer that Windows PowerShell 5.1
else {
Write-Verbose "Using legacy code"
#need to account for errors when accessing folders without permissions
#a function to recurse and get all non-hidden directories
Function _enumdir {
[cmdletbinding()]
Param([string]$Path, [switch]$Hidden)
# write-host $path -ForegroundColor cyan
$path = Convert-Path -literalpath $path
$ErrorActionPreference = "Stop"
try {
$di = [System.IO.DirectoryInfo]::new($path)
if ($hidden) {
$top = $di.GetDirectories()
}
else {
$top = ($di.GetDirectories()).Where( {$_.attributes -notmatch "hidden"})
}
$top
foreach ($t in $top) {
$params = @{
Path = $t.fullname
Hidden = $Hidden
}
_enumdir @params
}
}
Catch {
Write-Warning "Failed on $path. $($_.exception.message)."
}
} #enumdir
# get files in the root of the folder
if ($hidden) {
Write-Verbose "Including hidden files"
$data = $d.GetFiles()
}
else {
#get files in current location
$data = $($d.GetFiles()).Where({$_.attributes -notmatch "hidden"})
}
if ($data.count -gt 1) {
$files.AddRange($data)
}
elseif ($data.count -eq 1) {
[void]($files.Add($data))
}
#get a list of all non-hidden subfolders
Write-Verbose "Getting subfolders (Hidden = $Hidden)"
$eParam = @{
Path = $cpath
Hidden = $hidden
}
$all = _enumdir @eparam
#get the files in each subfolder
Write-Verbose "Getting files from $($all.count) subfolders"
($all).Foreach( {
Write-Verbose $_.fullname
$ErrorActionPreference = "Stop"
Try {
if ($hidden) {
$data = (([System.IO.DirectoryInfo]"$($_.fullname)").GetFiles())
}
else {
$data = (([System.IO.DirectoryInfo]"$($_.fullname)").GetFiles()).where({$_.Attributes -notmatch "Hidden"})
}
if ($data.count -gt 1) {
$files.AddRange($data)
}
elseif ($data.count -eq 1) {
[void]($files.Add($data))
}
}
Catch {
Write-Warning "Failed on $path. $($_.exception.message)."
#clearing the variable as a precaution
Clear-variable data
}
})
} #else 5.1
If ($files.count -gt 0) {
Write-Verbose "Found $($files.count) files"
# there appears to be a bug with the array list in Windows PowerShell
# where it doesn't always properly enumerate. Passing the list
# items via ForEach appears to solve the problem and doesn't
# adversely affect PowerShell 7. Addeed in v2.22.0. JH
$stats = $files.foreach( {$_}) | Measure-Object -property length -sum
$totalFiles = $stats.count
$totalSize = $stats.sum
}
else {
Write-Verbose "Found an empty folder"
$totalFiles = 0
$totalSize = 0
}
[pscustomobject]@{
PSTypename = "FolderSizeInfo"
Computername = [System.Environment]::MachineName
Path = $cPath
Name = $(Split-Path $cpath -leaf)
TotalFiles = $totalFiles
TotalSize = $totalSize
}
} #test path
else {
Write-Warning "Can't find $Path on $([System.Environment]::MachineName)"
}
} #foreach item
} #process
End {
Write-Verbose "Ending $($MyInvocation.MyCommand)"
}
} #close function
<#
C:\Windows from Explorer
Contains 206345 files
contains 61817 folders
total size 30.6gb
size on disk 30.7 gb
#>