-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-DirectorySize.psm1
More file actions
97 lines (93 loc) · 3.78 KB
/
Copy pathGet-DirectorySize.psm1
File metadata and controls
97 lines (93 loc) · 3.78 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
# TODO: Add tree option
# TODO: Automatically filter out files (useful when using `Get-ChildItems | Get-Directory Size`)
function Get-DirectorySize {
<#
.SYNOPSIS
Gets the total size of the directory
.DESCRIPTION
Recursively scans a directory, gets a sum of all file sizes (using
the Length parameter), and returns the value in a specified format.
.PARAMETER Path
Specifies the directory
.PARAMETER SizeFormat
Specifies the format of the size preOutput
.PARAMETER Value
Returns the value only rather than a hash table with
formatted strings
.OUTPUTS
If -Value is set, returns a System.Double
Otherwise, returns a System.String
#>
param (
[PSDefaultValue(Help='Current directory')]
[Parameter(Position=0,ValueFromPipeline)]
[ValidateScript(
{Test-Path $_}
# ErrorMessage = "`n{0} is not a valid path. Validate that the path exists and try again."
)]
[string]$Path = $PWD.Path,
[ValidateSet('B','KB','MB','GB','TB','Auto')]
[PSDefaultValue(Help='Auto')]
[string]$SizeFormat = 'Auto'
)
begin {
$preOutput = @()
Write-Debug "`$Value = $Value"
Write-Debug "`$preOutput type before is $($preOutput.GetType())"
}
process {
$allFolders = Get-ChildItem $Path -Directory
$allFolders | ForEach-Object {
$obj = [PSCustomObject]@{
PSTypeName = "Custom.DirectorySize"
Name = $_.BaseName
Length = Get-ChildItem $_ -Recurse | Where-Object length -ne $null |
Select-Object -ExpandProperty Length | Measure-Object -sum |
Select-Object -ExpandProperty Sum
}
# $evalSize = Invoke-Expression "$($dirSize.toString())/1$SizeFormat"
Write-Debug "`$_ = $_"
# Write-Debug "`$evalSize = $evalSize`n"
# Write-Debug "$($_.Name), $evalSize"
Add-Member -InputObject $obj -MemberType NoteProperty -Name KB -Value $($obj.Length/1kb)
Add-Member -InputObject $obj -MemberType NoteProperty -Name MB -Value $($obj.Length/1mb)
Add-Member -InputObject $obj -MemberType NoteProperty -Name GB -Value $($obj.Length/1gb)
Add-Member -InputObject $obj -MemberType NoteProperty -Name TB -Value $($obj.Length/1tb)
$preOutput += $obj
}
}
end {
if ($SizeFormat -eq "Auto") {
$avg = $preOutput | Measure-Object -Property Length -Average | Select-Object -ExpandProperty Average
$SizeType = switch ($avg) {
{$_ -ge 1gb} {
"GB"
continue
}
{$_ -ge 1mb} {
"MB"
continue
}
{$_ -ge 1kb} {
"KB"
continue
}
Default {
"Length"
}
}
$output = @()
$preOutput | ForEach-Object {
$outObj = $_
Add-Member -InputObject $outObj -MemberType AliasProperty -Name "Size ($SizeType)" -Value $SizeType -Force
$output += $outObj
}
}
Remove-TypeData -TypeName Custom.DirectorySize -ErrorAction SilentlyContinue
Update-TypeData -TypeName Custom.DirectorySize -DefaultDisplayPropertySet "Name","Size ($SizeType)"
Write-Host "Directory sizes in $Path" -ForegroundColor Green
Write-Host "Total size: $(($output."$SizeType" | Measure-Object -Sum).Sum) $SizeType" -ForegroundColor Blue
$output
}
}
Set-Alias -Name gds -Value Get-DirectorySize