-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-FileSizes.ps1
298 lines (232 loc) · 10.7 KB
/
Get-FileSizes.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#region ScriptInfo
<#
.SYNOPSIS
Gets file sizes from a specified directory and outputs results to a .csv file.
.DESCRIPTION
Gets file sizes from a specified directory and outputs results to a .csv file. Details gathered include File Path, Directory Name, File Name, Size (Bytes), Size (MB), Size (GB), Extension, Creation Time (UTC),
Last Write Time (UTC) and Read Only.
.PARAMETER outDir
This is the directory where the csv report will be saved to. If the directory doesn't exist it will be created.
.PARAMETER scanDir
This is the directory to scan to generate the report for file sizes.
.PARAMETER roundTo
[OPTIONAL] This is the number of decimal points the file sizes will be calculated to. The default is 0 and the choices are 0, 1, 2, 3 & 4 decimal places.
.PARAMETER filesBiggerThan
[OPTIONAL] This parameter is used to limit the scan to files bigger than the set size in GB. Cannot be used with the -filesLessThan parameter.
.PARAMETER filesLessThan
[OPTIONAL] This parameter is used to limit the scan to files less than the set size in GB. Cannot be used with the -filesBiggerThan parameter.
.PARAMETER sortBy
[OPTIONAL] This parameter is used to specify the sort order for the report, the default is File Patch. The choices are File Path, Big Files & Small Files.
.EXAMPLE
.\Get-FileSizes.ps1 -outDir "c:\temp" -scanDir "C:\Windows"
Scans C:\Windows and saves the .csv report to c:\temp
.EXAMPLE
.\Get-FileSizes.ps1 -outDir "c:\temp" -scanDir "C:\Windows" -fileBiggerThan 1 -roundTo 2
Scans C:\Windows for files larger than 1 GB and saves the .csv report to c:\temp. The file sizes will be rounded to 2 decimal places.
.EXAMPLE
.\Get-FileSizes.ps1 -outDir "c:\temp" -scanDir "C:\Windows" -fileLessThan 5
Scans C:\Windows for files less than 5 GB and saves the .csv report to c:\temp
.EXAMPLE
.\Get-FileSizes.ps1 -outDir "c:\temp" -scanDir "C:\Windows" -fileBiggerThan 1 -orderBy BigFiles
Scans C:\Windows for files larger than 1 GB and saves the .csv report to c:\temp. The report will be ordered by the biggest files.
.LINK
https://github.com/gordonrankine/get-filesizes
.NOTES
License: MIT License
Compatibility: Windows 10
Author: Gordon Rankine
Date: 05/11/2021
Version: 1.0
PSScriptAnalyzer: Pass.
Change Log: Version Date Author Comments
1.0 05/11/2021 Gordon Rankine Initial script
#>
#endregion ScriptInfo
#region Bindings
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True, Position=1, HelpMessage="This is the directory where the .cvs report file will be saved.")]
[string]$outDir,
[Parameter(Mandatory=$True, Position=2, HelpMessage="This is the directory to scan for file sizes.")]
[string]$scanDir,
[Parameter(Mandatory=$False, Position=3, HelpMessage="This is the amount of decimal places the files sizes will be in. The default is 0 and you can choose between 0 & 4.")]
[ValidateSet(0,1,2,3,4)]
[int]$roundTo = 0,
[Parameter(Mandatory=$False, Position=4, HelpMessage="Use this to scan for files bigger than x GB, where x is the number of GB.")]
[long]$filesBiggerThan,
[Parameter(Mandatory=$False, Position=5, HelpMessage="Use this to scan for files less than x GB, where x is the number of GB.")]
[long]$filesLessThan,
[Parameter(Mandatory=$False, Position=6, HelpMessage="Use this to sort the date either by File Path, Big Files or Small Files. The default is File Path")]
[ValidateSet('FilePath','BigFiles', 'SmallFiles')]
[string]$sortBy = 'FilePath'
)
#endregion Bindings
#region Functions
### START FUNCTIONS ###
function fnCreateDir {
<#
.SYNOPSIS
Creates a directory.
.DESCRIPTION
Creates a directory.
.PARAMETER outDir
This is the directory to be created.
.EXAMPLE
.\Create-Directory.ps1 -outDir "c:\test"
Creates a directory called "test" in c:\
.EXAMPLE
.\Create-Directory.ps1 -outDir "\\COMP01\c$\test"
Creates a directory called "test" in c:\ on COMP01
.LINK
https://github.com/gordonrankine/powershell
.NOTES
License: MIT License
Compatibility: Windows 7 or Server 2008 and higher
Author: Gordon Rankine
Date: 13/01/2019
Version: 1.1
PSSscriptAnalyzer: Pass
#>
[CmdletBinding()]
Param(
# The directory to be created.
[Parameter(Mandatory=$True, Position=0, HelpMessage='This is the directory to be created. E.g. C:\Temp')]
[string]$outDir
)
# Create out directory if it doesnt exist
if(!(Test-Path -path $outDir)){
if(($outDir -notlike "*:\*") -and ($outDir -notlike "*\\*")){
Write-Output "[ERROR] $outDir is not a valid path. Script terminated."
break
}
try{
New-Item $outDir -type directory -Force -ErrorAction Stop | Out-Null
Write-Output "[INFO] Created output directory $outDir"
}
catch{
Write-Output "[ERROR] There was an issue creating $outDir. Script terminated."
Write-Output ($_.Exception.Message)
Write-Output ""
break
}
}
# Directory already exists
else{
Write-Output "[INFO] $outDir already exists."
}
} # end fnCreateDir
function fnCheckPSAdmin {
<#
.SYNOPSIS
Checks PowerShell is running as Administrator.
.DESCRIPTION
Checks PowerShell is running as Administrator.
.LINK
https://github.com/gordonrankine/powershell
.NOTES
License: MIT License
Compatibility: Windows 7 or Server 2008 and higher
Author: Gordon Rankine
Date: 19/09/2019
Version: 1.0
PSSscriptAnalyzer: Pass
#>
try{
$wIdCurrent = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$wPrinCurrent = New-Object System.Security.Principal.WindowsPrincipal($wIdCurrent)
$wBdminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
if(!$wPrinCurrent.IsInRole($wBdminRole)){
Write-Output "[ERROR] PowerShell is not running as administrator. Script terminated."
Break
}
}
catch{
Write-Output "[ERROR] There was an unexpected error checking if PowerShell is running as administrator. Script terminated."
Break
}
} # end fnCheckPSAdmin
#endregion Functions
#region StartCollecting
Clear-Host
# Start stopwatch
$sw = [system.diagnostics.stopwatch]::StartNew()
fnCheckPSAdmin
fnCreateDir $outDir
# Date and Endpoint used to construct $outFile
$date = Get-Date -UFormat %Y%m%d%H%M
$endpoint = $env:COMPUTERNAME
$outFile = "$outDir\GetFileSizes_$($endpoint)_$($date).csv"
if($filesBiggerThan -ne '' -and $filesLessThan -ne ''){
Write-Output "[ERROR] -filesBiggerThan and -filesLessThan can't be run togther. Please choose one only and try again."
Write-Output ""
break
}
# Create File array to store results then get files.
# Script will be running as admin but there may be some files even the admin cant see. These will be skipped.
$filesArray = @()
try{
Write-Output "[INFO] File size rounding set to $($roundTo) decimal places."
Write-Output "[INFO] Ordering set to $($SortBy)."
if($filesBiggerThan -ne ''){
Write-Output "[INFO] Scanning for files bigger than $($filesBiggerThan) GB."
Write-Output "[INFO] Scanning $($scanDir), please wait..."
$filesBiggerThan = $filesBiggerThan * 1073741824
$files = Get-ChildItem -Path $scanDir -File -Recurse -ErrorAction SilentlyContinue -Force | Where-Object {$_.Length -ge $filesBiggerThan}
}
elseif($filesLessThan -ne ''){
Write-Output "[INFO] Scanning for files less than $($filesLessThan) GB."
Write-Output "[INFO] Scanning $($scanDir), please wait..."
$filesLessThan = $filesLessThan * 1073741824
$files = Get-ChildItem -Path $scanDir -File -Recurse -ErrorAction SilentlyContinue -Force | Where-Object {$_.Length -le $filesLessThan}
}
else{
Write-Output "[INFO] Scanning $($scanDir), please wait..."
$files = Get-ChildItem -Path $scanDir -File -Recurse -ErrorAction SilentlyContinue -Force
}
}
catch{
Write-Output "[ERROR] There was an issue scanning $($scanDir). Script terminated."
Write-Output ($_.Exception.Message)
Write-Output ""
break
}
# Get file properties for each file and do some calculations
foreach($file in $files){
$fileArray = New-Object System.Object
$fileArray | Add-Member -type NoteProperty -name "File Path" -Value $file.FullName -Force
$fileArray | Add-Member -type NoteProperty -name DirectoryName -Value $file.DirectoryName -Force
$fileArray | Add-Member -type NoteProperty -name Name -Value $file.Name -Force
$fileArray | Add-Member -type NoteProperty -name "Size (Bytes)" -Value $file.Length -Force
$fileArray | Add-Member -type NoteProperty -name "Size (MB)" -Value ([math]::Round($file.Length/1048576,$roundTo)) -Force
$fileArray | Add-Member -type NoteProperty -name "Size (GB)" -Value ([math]::Round($file.Length/1073741824,$roundTo)) -Force
$fileArray | Add-Member -type NoteProperty -name Extension -Value $file.Extension -Force
$fileArray | Add-Member -type NoteProperty -name "Creation Time (UTC)" -Value $file.CreationTimeUtc -Force
$fileArray | Add-Member -type NoteProperty -name "Last Write Time (UTC)" -Value $file.LastWriteTimeUtc -Force
$fileArray | Add-Member -type NoteProperty -name "Read Only" -Value $file.IsReadOnly -Force
$filesArray += $fileArray
}
#endregion StartCollecting
#region GenerateReports
try{
Write-Output "[INFO] Saving report file."
if($SortBy -eq 'BigFiles'){
$filesArray | Sort-Object "Size (Bytes)" -Descending | Export-Csv $outFile -Encoding ASCII -NoTypeInformation -Force
}
elseif($SortBy -eq 'SmallFiles'){
$filesArray | Sort-Object "Size (Bytes)" | Export-Csv $outFile -Encoding ASCII -NoTypeInformation -Force
}
else{
$filesArray | Export-Csv $outFile -Encoding ASCII -NoTypeInformation -Force
}
}
catch{
Write-Output "[ERROR] There was an issue saving $($outFile). Script terminated."
Write-Output ($_.Exception.Message)
Write-Output ""
break
}
# Display script execution time
Write-Output "[INFO] Script complete in $($sw.Elapsed.Hours) hours, $($sw.Elapsed.Minutes) minutes, $($sw.Elapsed.Seconds) seconds."
Write-Output "[INFO] Results can be found at $($outfile)"
Write-Output ""
#endregion GenerateReportsg