Variablen und Typen
PowerShell ist dynamisch typisiert, nutzt aber .NET-Typen. Du kannst einen Typ festlegen:
$name = "Mia"
[int]$alter = 17
[double]$groesse = 1.68
$aktiv = $true
$nichts = $null
"{0} {1} {2} {3}" -f $name.GetType().Name, $alter.GetType().Name, $groesse.GetType().Name, $aktiv.GetType().Name
$x = 5
$x += 10
$x *= 2
$x
$a, $b = 1, 2
$a, $b = $b, $a
"$a $b"
$text = "42"
$text + 1
1 + $text
[int]$text + 1
"5" * 3
"abc".Length
[int]"12" + [int]"30"
"3.7" -as [double]
"x" -as [int]
$null -eq $nichtsAusgabe
String Int32 Double Boolean 30 2 1 421 43 43 555 3 42 3.7 True
Variablen mit $ am Anfang; automatische Variablen sind u. a. $_ (aktuelles Pipeline-Objekt), $true, $false, $null, $args, $PSVersionTable, $HOME, $PWD, $env:PATH (Umgebungsvariablen).
Strings
$s = "Hallo Welt"
$name = "Mia"
"Name: $name, Länge: $($s.Length), doppelt: $($s * 2)"
'Einfache Anführungszeichen: $name bleibt'
"Tab:`tZeilenumbruch:`nnächste Zeile" # Escape-Zeichen ist der Backtick
$s.ToUpper(); $s.ToLower(); $s.Substring(0, 5); $s.Replace("Welt", "PowerShell")
$s.Contains("Welt"); $s.StartsWith("Hal"); $s.IndexOf("W")
$s.Split(" ") -join "|"
"a,b,,c".Split(",").Count
" trim ".Trim() + "|"
$s -replace "l", "L"
$s -match "W(\w+)"; $Matches[1]
"2025-03-15" -split "-"
-join ("a", "b", "c")
"{0:N2} | {1:P0} | {2:D5} | {3:yyyy-MM-dd}" -f 3.14159, 0.256, 42, (Get-Date -Year 2025 -Month 3 -Day 15)
"{0,8}|{1,-8}|" -f "rechts", "links"
$mehrzeilig = @"
Zeile 1 mit $name
Zeile 2
"@
$mehrzeilig
[string]::Join(", ", 1..4)
[string]::IsNullOrEmpty("")
"abc".PadLeft(5, '*') + "abc".PadRight(5, '-')Ausgabe
Name: Mia, Länge: 10, doppelt: Hallo WeltHallo Welt Einfache Anführungszeichen: $name bleibt Tab: Zeilenumbruch: nächste Zeile HALLO WELT hallo welt Hallo Hallo PowerShell True True 6 Hallo|Welt 4 trim| HaLLo WeLt True elt 2025 03 15 abc 3.14 | 26 % | 00042 | 2025-03-15 rechts|links | Zeile 1 mit Mia Zeile 2 1, 2, 3, 4 True **abcabc--
Operatoren
| Typ | Operatoren |
|---|---|
| Arithmetik | + - * / % (kein **; Potenz: [math]::Pow(2, 10)) |
| Vergleich | -eq -ne -gt -ge -lt -le (nicht ==, <!) |
| Text-Vergleich | -like (Wildcards), -match (Regex), -contains, -in |
| Logik | -and -or -not -xor (! auch erlaubt) |
| Text | -replace -split -join -f (Format) |
| Typ | -is -isnot -as |
7 / 2; [math]::Floor(7 / 2); 7 % 3; [math]::Pow(2, 10); [math]::Sqrt(144); [math]::Round(2.5); [math]::Round(3.14159, 2)
5 -gt 3; 5 -eq 5.0; "abc" -eq "ABC"; "abc" -ceq "ABC"
"Hallo" -like "H*"; "Hallo" -notlike "*x*"; "Hallo" -match "^H.l+o$"
1, 2, 3 -contains 2; 2 -in 1, 2, 3
(5 -gt 3) -and (2 -lt 4); -not $true
"text" -is [string]; 5 -is [int]; 5 -isnot [string]
[math]::Max(3, 9); [math]::Abs(-5); [int]::MaxValue; [long]::MaxValue
[datetime]"2025-03-15"
([datetime]"2025-12-24" - [datetime]"2025-03-15").Days
(Get-Date -Year 2025 -Month 3 -Day 15).AddDays(20).ToString("yyyy-MM-dd")
0.1 + 0.2 -eq 0.3
[decimal]0.1 + [decimal]0.2 -eq [decimal]0.3Ausgabe
3.5 3 1 1024 12 2 3.14 True True True False True True True True True True False True True True 9 5 2147483647 9223372036854775807 284 2025-04-04 False True
Merke
- Variablen mit
$, optional typisiert ([int]$x); .NET-Typen und -Methoden stehen zur Verfügung - Strings:
"..."ersetzt Variablen und$(...),'...'wörtlich; Escape-Zeichen ist der Backtick - Vergleich mit
-eq -ne -gt -lt -like -match -in -contains - Format-Operator
-fund Regex-Operatoren-replace,-split,-match [math]::und[datetime]für Mathematik und Datum
Aufgabe
Berechne, wie viele Tage bis zum 24. Dezember fehlen, und gib das Ergebnis formatiert aus.