-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirSize.ps1
More file actions
57 lines (46 loc) · 1.85 KB
/
Copy pathDirSize.ps1
File metadata and controls
57 lines (46 loc) · 1.85 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
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string] $Path,
[Parameter(Mandatory=$false)]
[switch] $Detailed,
[Parameter(Mandatory=$false)]
[switch] $Mb
)
function Get-DirectorySize {
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string] $Path,
[Parameter(Mandatory=$false)]
[bool] $Detailed,
[Parameter(Mandatory=$false)]
[bool] $Mb
)
if($Detailed) {
$total_file_count = 0
$total_file_size = 0
Get-ChildItem $Path -recurse | ?{ $_.PSIsContainer } | %{
try {
$file_count = [System.IO.Directory]::GetFiles($_.FullName, "*", "AllDirectories").Count
$file_size = 0
if($Mb) {
$file_size = ((Get-ChildItem $_.FullName -recurse | Measure-Object Length -Sum).sum / 1Mb)
"{0:N2} MB | $file_count files `t $_" -f $file_size
} else {
$file_size = ((Get-ChildItem $_.FullName -recurse | Measure-Object Length -Sum).sum / 1Gb)
"{0:N2} GB | $file_count files `t $_" -f $file_size
}
}
catch { }
}
}
$total_file_count = [System.IO.Directory]::GetFiles($Path, "*", "AllDirectories").Count
$total_file_size = (([System.IO.DirectoryInfo]$Path).EnumerateFiles("*", 'AllDirectories').Length)
if($Mb) {
"{0:N2} MB | {1:N0} files `t $Path" -f (($total_file_size | Measure-Object -Sum).Sum / 1Mb), $total_file_count
} else {
"{0:N2} GB | {1:N0} files `t $Path" -f (($total_file_size | Measure-Object -Sum).Sum / 1Gb), $total_file_count
}
}
Get-DirectorySize -Path $Path -Detailed $Detailed -Mb $Mb