Public/Get-DirectorySize.ps1

<#
 
# Get-DirectorySize
 
Ohne Beschreibung
 
- **Hashtags** UserCmdlet
- **Version** 2020.12.9
 
#>


using namespace System
using namespace System.IO
using namespace System.Management.Automation

Set-StrictMode -Version Latest

function Get-DirectorySize {
    <#
    .SYNOPSIS
        Zeigt den belegt Ordner-Platz an.
    .EXAMPLE
        Get-DirectorySize -Path $env:USERPROFILE\.vscode\extensions
    #>

    param (
        # Der Order ab dem die Ordner-Größen gemessen werden soll.
        [ValidateScript({
            $isExistContainer = Test-Path -Path $_ -PathType Container
            if(-not $isExistContainer) {
                throw "Path '$_' does not exist."
            }

            $pathObject = Get-Item -Path $_
            if(-not ($pathObject -is [DirectoryInfo])) {
                throw "Path $_ is not a [DirectoryInfo]-Object."
            }

            return $true
        })]
        [String]
        $Path = '.',

        # Dateien ausschließen die kleiner als X Bytes sind.
        [ValidateRange(0, 1GB)]
        [Int64]
        $MinimumFileSize = 0,

        # Order in der ÜBersicht nicht anzeigen die kleiner als X Bytes sind.
        [ValidateRange(0, 1PB)]
        [Int64]
        $MinimumDirectorySize = 0,

        # Schließt Versteckte- und System-Dateien mit ein.
        [SwitchParameter]
        $Force
    )

    $My = [HashTable]::Synchronized(@{})

    $My.TotalDirectorySize = 0
    $My.BaseDirectoryCount = 0
    $My.BaseDirectories = Get-ChildItem -Path $Path -Directory -ErrorAction 'Ignore'
    $My.Multiplikator = 100/$My.BaseDirectories.Count

    $My.BaseDirectories | ForEach-Object -Process {
        $My.BaseDirectoryCount++
        $My.DirectoryMeasure = Get-ChildItem -Path $_.FullName -File -Force:$Force -Recurse -ErrorAction 'Ignore' | Where-Object -Property Length -GE -Value $MinimumFileSize | Measure-Object -Property 'Length' -Sum
        $My.Result = [PSCustomObject]@{
            Size     = $My.DirectoryMeasure.Sum
            FullName = $_.FullName
        }
        $My.TotalDirectorySize += $My.Result.Size

        $param = @{
            Activity         = 'Analyse Directory Size'
            Status           = "Actual total directory size over {0} is {1:#,##0} GB." -f $Path, [Math]::Round($My.TotalDirectorySize / 1GB, 2)
            CurrentOperation = "Calculate directory '$($_.Name)'"
            PercentComplete  = [Math]::Floor($My.BaseDirectoryCount * $My.Multiplikator)
        }
        Write-Progress @param

        if($My.Result.Size -gt $MinimumDirectorySize) {
            return $My.Result
        }

    } | Sort-Object -Property 'Size' -Descending | ForEach-Object -Process {
            $My.AllocateBar = '▓' *       (($_.Size / $My.TotalDirectorySize) * 25)
            $My.FreeBar     = ' ' * (25 - (($_.Size / $My.TotalDirectorySize) * 25))
            return [PSCustomObject]@{
                Name       = Split-Path -Path $_.FullName -Leaf
                Allocation = "▕{0}{1}▏{2,7:0.0 %}" -f $My.FreeBar, $My.AllocateBar, ($_.Size / $My.TotalDirectorySize)
                SizeMB     = [Math]::Ceiling($_.Size / 1MB)
                FullName   = $_.FullName
            } | Add-Member -TypeName 'DirectorySize' -PassThru
        }

    Write-Progress -Activity 'Analyse Directory Size' -Completed
    "{1:#,##0} GB total Directory-Size over '{0}'" -f $Path, [Math]::Round($My.TotalDirectorySize / 1GB, 2) | Write-Warning

    Remove-Variable -Name My -Force -ErrorAction Ignore
}

Set-StrictMode -Off