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.
    .PARAMETER Path
        Der Order ab dem die Ordner-Größen gemessen werden soll.
    .EXAMPLE
        Get-DirectorySize -Path $env:USERPROFILE\.vscode\extensions
    #>

    param (
        [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 = '.'
    )
    $totalSum = 0
    $counter = 0
    $BaseDirectories = Get-ChildItem -Path $Path -Directory -ErrorAction 'Ignore'
    $multiplikator = 100/$BaseDirectories.Count

    $BaseDirectories | ForEach-Object -Process {
        $counter++
        $result = [PSCustomObject]@{
            Size     = Get-ChildItem -Path $_.FullName -File -Force -Recurse -ErrorAction 'Ignore' | Measure-Object -Property 'Length' -Sum | Select-Object -ExpandProperty 'Sum'
            FullName = $_.FullName
        }
        $totalSum += $result.Size

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

        return $result } | Sort-Object -Property 'Size' -Descending | ForEach-Object -Process {
            $allocateBar = '▓' *       (($_.Size / $totalSum) * 25)
            $freeBar     = ' ' * (25 - (($_.Size / $totalSum) * 25))
            return [PSCustomObject]@{
                Name       = Split-Path -Path $_.FullName -Leaf
                Allocation = "▕{0}{1}▏{2,7:0.0 %}" -f $freeBar, $allocateBar, ($_.Size / $totalSum)
                SizeMB     = [Math]::Ceiling($_.Size / 1MB)
                FullName   = $_.FullName
            }
        }

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

Set-StrictMode -Off