Format-FileSize #
ÜBERSICHT #
Formatiert eine Byte-Anzahl kompakt als B/kB/MB/GB/TB mit optionalen Nachkommastellen und wahlweise US-Zahlenformat (en-US).
SYNTAX #
Format-FileSize [-Size] <long> [[-Decimals] <int>] [-US] [<CommonParameters>]
BESCHREIBUNG #
Konvertiert eine Größe in Bytes ([long]) in einen gut lesbaren String mit Einheit. Die Schwellen basieren auf Binärpräfixen (1KB = 1024 B, 1MB = 1024 KB, …). Negative Werte werden mit führendem Minus ausgegeben. Die Funktion ist pipelinefähig und akzeptiert:
- nackte Zahlen (ValueFromPipeline)
- Objekte mit Eigenschaften ‘Length’ oder ‘Bytes’ (ValueFromPipelineByPropertyName)
Standardmäßig wird die aktuelle UI-Kultur zur Zahlformatierung verwendet (z. B. „4,33 GB“ unter de-DE). Mit -US lässt sich en-US erzwingen („4.33 GB“).
PARAMETER #
-Size #
Type: long
Byte-Anzahl, die formatiert werden soll. Akzeptiert [long] direkt aus der Pipeline oder per Property-Bindung (z. B. FileInfo.Length).
-Decimals #
Type: int
Default: 0
Anzahl der Nachkommastellen für kB/MB/GB/TB. Standard: 0.
-US #
Type: switch
Erzwingt US-Formatierung (en-US) mit Punkt als Dezimaltrenner und Komma als Tausendertrennzeichen.
.INPUTS long Sowie Objekte mit den Properties ‘Length’ oder ‘Bytes’.
AUSGABEN #
string
Formatierte Größe inkl. Einheit (z. B. “4 GB”, “4,33 GB”, “512 B”).
BEISPIELE #
# Standardausgabe ohne Nachkommastellen
Format-FileSize -Size 4654644444
4 GB
# Mit Nachkommastellen (kulturabhängig, hier de-DE)
Format-FileSize -Size 4654644444 -Decimals 2
4,33 GB
# US-Format (erzwingt en-US)
Format-FileSize -Size 4654644444 -Decimals 2 -US
4.33 GB
# Pipeline: nackte Zahl
123456789 | Format-FileSize -Decimals 1
117,7 MB
# Pipeline by property: Dateigrößen aus Get-ChildItem schön formatiert
Get-ChildItem C:\Temp | Select-Object Name,@{N='Size';E={ Format-FileSize -Size $_.Length -Decimals 2 }}
# -> z. B. "1,23 MB"
HINWEISE #
- Einheiten-Labels: “kB”, “MB”, “GB”, “TB”
- Bereich: Int64 → bis ~9,22 EB
- 0 Bytes wird als “0 B” ausgegeben