forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-FileExtensionInfo.ps1
More file actions
91 lines (78 loc) · 3.23 KB
/
Copy pathGet-FileExtensionInfo.ps1
File metadata and controls
91 lines (78 loc) · 3.23 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
#requires -version 7.1
using namespace System.Collections.generic
Function Get-FileExtensionInfo {
[cmdletbinding()]
[alias("gfei")]
[OutputType("FileExtensionInfo")]
Param(
[Parameter(Position = 0, HelpMessage = "Specify the root directory path to search")]
[ValidateNotNullorEmpty()]
[ValidateScript( { Test-Path $_ })]
[string]$Path = ".",
[Parameter(HelpMessage = "Recurse through all folders.")]
[switch]$Recurse,
[Parameter(HelpMessage = "Include files in hidden folders")]
[switch]$Hidden,
[Parameter(HelpMessage = "Add the corresponding collection of files")]
[Switch]$IncludeFiles
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
#convert the path to a file system path
$cPath = Convert-Path -Path $Path
#capture the current date and time for the audit date
$report = Get-Date
Try {
$enumOpt = [System.IO.EnumerationOptions]::new()
}
Catch {
Throw "This commands requires PowerShell 7."
}
if ($Recurse) {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Getting files recursively"
}
$enumOpt.RecurseSubdirectories = $Recurse
if ($Hidden) {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Including hidden files"
$enumOpt.AttributesToSkip -= 2
}
#initialize a list to hold the results
$list = [list[object]]::new()
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $cPath"
$dir = Get-Item -Path $cpath
$files = $dir.getfiles('*', $enumOpt)
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Found $($files.count) files"
$group = $files | Group-Object -Property extension
#Group and measure
foreach ($item in $group) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Measuring $($item.count) $($item.name) files"
$measure = $item.Group | Measure-Object -Property length -Minimum -Maximum -Average -Sum
#create a custom object
$out = [PSCustomObject]@{
PSTypeName = "FileExtensionInfo"
Path = $cPath
Extension = $item.Name
Count = $item.Count
TotalSize = $measure.Sum
SmallestSize = $measure.Minimum
LargestSize = $measure.Maximum
AverageSize = $measure.Average
Computername = [system.environment]::MachineName
ReportDate = $report
Files = $IncludeFiles ? $item.group : $null
IsLargest = $False
}
$list.Add($out)
}
} #process
End {
#mark the extension with the largest total size
($list | Sort-Object -Property TotalSize,Count)[-1].IsLargest = $true
#write the results to the pipeline
$list
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
Update-TypeData -TypeName FileExtensionInfo -MemberType AliasProperty -MemberName Total -Value TotalSize -Force