public/Measure-Directory.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
<#
    .Synopsis
        Calculate the size of files and directories at a location.
    .Description
        Calculate the size of files and directories at a location.
    .Example
        PS C:\> Measure-Directory -Path C:\Windows\Temp
        Calculate the size of files and directories at a location.
#>

function Measure-Directory {
    [CmdletBinding()]
    param (
        # The directory to measure.
        [Parameter(Mandatory = $true)]
        [String]
        $Path,
        # The list of files to exclude.
        [Parameter(Mandatory = $false)]
        [String[]]
        $Exclude = @('.gitkeep'),
        # The unit usd to display the file and directory sizes.
        [Parameter(Mandatory = $false)]
        [ValidateSet('KB', 'MB', 'GB')]
        [String]
        $Unit = 'MB'
    )

    begin {
        Write-LogMessage -Message "Started execution"
    }

    process {
        if (Test-Path -Path $Path) {
            Write-LogMessage -Message "Measuring '$Path'"
            Get-ChildItem -Path $Path -Include * -Exclude $Exclude | ForEach-Object {
                if ($_.PSIsContainer) {
                    $type = "Directory"
                    $size = Get-ChildItem -Path $_.FullName -Recurse |
                        Measure-Object -Property Length -Sum |
                        Select-Object -ExpandProperty Sum
                }
                else {
                    $type = "File"
                    $size = $_.Length
                }

                return [PSCustomObject] @{
                    Type = $type
                    Size = $size
                    Item = "$($_.Name)"
                }
            } | Format-Table -Property @(
                "Type",
                @{
                    Name       = "Size ($Unit)"
                    Expression = { "{0:N0}" -f ($_.Size / "1$Unit") }
                    Align      = "Right"
                },
                "Item"
            )
        }
    }

    end {
        Write-LogMessage -Message "Finished execution"
    }
}