EMZETT.
Login

Kurz: Dateisystem, Registry, Umgebungsvariablen und mehr sind “Laufwerke” (Provider) mit denselben Cmdlets:

Teil des Kurses PowerShell

Kapitel 6 von 8 im Kurs PowerShell (Abschnitt „Alltag“). Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.

Dateisystem

Dateisystem, Registry, Umgebungsvariablen und mehr sind “Laufwerke” (Provider) mit denselben Cmdlets:

$dir = Join-Path ([IO.Path]::GetTempPath()) "psdemo_$(Get-Random)"
New-Item -ItemType Directory -Path $dir | Out-Null
Set-Location $dir
"Zeile 1`nZeile 2`nZeile 3" | Set-Content "notiz.txt"
Add-Content "notiz.txt" "Zeile 4"
Get-Content "notiz.txt" | Measure-Object -Line | ForEach-Object Lines
(Get-Content "notiz.txt")[1]
Get-Content "notiz.txt" -TotalCount 2
Test-Path "notiz.txt"; Test-Path "gibt-es-nicht"
New-Item -ItemType Directory "unter" | Out-Null
Copy-Item "notiz.txt" "unter/kopie.txt"
Rename-Item "unter/kopie.txt" "umbenannt.txt"
"a;b`n1;2`n3;4" | Set-Content "daten.csv"
Get-ChildItem | Sort-Object Name | ForEach-Object { "$($_.Name) ($($_.PSIsContainer ? 'Ordner' : "$($_.Length) Bytes"))" }
Get-ChildItem -Recurse -File | ForEach-Object { $_.FullName.Substring($dir.Length + 1) } | Sort-Object
(Import-Csv "daten.csv" -Delimiter ";" | Measure-Object a -Sum).Sum
Get-ChildItem *.txt | Select-String "Zeile 3" | ForEach-Object { "$($_.LineNumber): $($_.Line)" }
Remove-Item "unter" -Recurse
Set-Location ..
Remove-Item $dir -Recurse -Force
Test-Path $dir

Ausgabe:

4
Zeile 2
Zeile 1
Zeile 2
True
False
daten.csv (12 Bytes)
notiz.txt (32 Bytes)
unter (Ordner)
daten.csv
notiz.txt
unter/umbenannt.txt
4
3: Zeile 3
False
Cmdlet (Alias)Zweck
Get-ChildItem (ls, dir, gci)auflisten (-Recurse, -Filter, -File)
Get-Content (cat, gc)Datei lesen
Set-Content, Add-Contentschreiben, anhängen
New-Item, Remove-Item, Copy-Item, Move-Item, Rename-Itemanlegen, löschen, kopieren, verschieben
Test-Path, Join-Path, Split-PathPfade prüfen/bauen
Select-StringText suchen (wie grep)
Import-Csv, Export-Csv, ConvertFrom-Jsonstrukturierte Daten

Reguläre Ausdrücke und Text

$log = @"
2025-03-01 INFO  Server gestartet
2025-03-01 ERROR Datenbank nicht erreichbar
2025-03-02 INFO  Nutzer mia angemeldet
2025-03-02 WARN  Speicher knapp
2025-03-03 ERROR Zeitüberschreitung
"@ -split "`n"
 
$log | Where-Object { $_ -match "ERROR" }
($log | Select-String -Pattern "(\d{4}-\d\d-\d\d) (\w+)" | ForEach-Object { $_.Matches[0].Groups[2].Value } | Group-Object | Sort-Object Name | ForEach-Object { "$($_.Name)=$($_.Count)" }) -join ", "
$log | ForEach-Object { if ($_ -match "^(?<datum>\S+)\s+(?<stufe>\w+)") { $Matches.stufe } } | Sort-Object -Unique
[regex]::Matches("a1b22c333", "\d+").Value -join ","
"2025-03-15" -replace "(\d+)-(\d+)-(\d+)", '$3.$2.$1'
"CamelCaseText" -creplace "(?<!^)([A-Z])", '_$1'
$log.Count

Ausgabe:

2025-03-01 ERROR Datenbank nicht erreichbar
2025-03-03 ERROR Zeitüberschreitung
ERROR=2, INFO=2, WARN=1
ERROR
INFO
WARN
1,22,333
15.03.2025
Camel_Case_Text
5

Module und Befehle von anderen

PowerShell wird über Module erweitert (Sammlungen von Cmdlets). Die PowerShell Gallery (powershellgallery.com) hält tausende:

Get-Module -ListAvailable | Select-Object -First 1 -ExpandProperty Name | ForEach-Object { $_ -ne $null }
(Get-Command -Module Microsoft.PowerShell.Utility | Measure-Object).Count -gt 50
Get-Command ConvertTo-Json | ForEach-Object Source
(Get-Command Get-Date).Version.Major -ge 7

Ausgabe:

True
True
Microsoft.PowerShell.Utility
True
Find-Module -Name PSReadLine           # in der Gallery suchen
Install-Module -Name Pester -Scope CurrentUser   # installieren
Import-Module Pester
Get-Command -Module Pester

Bekannte Module: Az (Azure), Microsoft.Graph (Microsoft 365), ActiveDirectory, Pester (Tests), PSScriptAnalyzer (Stil), dbatools (SQL Server).

Remoting und Web

Enter-PSSession -ComputerName server01                      # interaktive Sitzung (WinRM oder SSH)
Invoke-Command -ComputerName s1, s2 -ScriptBlock { Get-Service sshd }
Invoke-Command -HostName linux01 -UserName mia -SSHTransport -ScriptBlock { uname -a }
 
Invoke-RestMethod https://api.github.com/repos/PowerShell/PowerShell | Select-Object name, stargazers_count
Invoke-WebRequest https://example.com | Select-Object StatusCode
Invoke-RestMethod -Method Post -Uri $url -Body (@{ a = 1 } | ConvertTo-Json) -ContentType "application/json"

Skripte planen und signieren

  • Windows: Aufgabenplanung (Register-ScheduledJob, schtasks), Linux/macOS: cron mit pwsh -File
  • Ausführungsrichtlinie: Get-ExecutionPolicy, Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
  • Zertifikat-Signatur mit Set-AuthenticodeSignature für Unternehmensumgebungen

Merke

  • Dateisystem-Cmdlets: Get-ChildItem, Get-Content, Set-Content, Copy-Item, Remove-Item, Test-Path
  • Select-String ersetzt grep; -match/-replace und [regex] für reguläre Ausdrücke
  • Module aus der PowerShell Gallery (Install-Module), z. B. Az, Pester
  • Remoting mit Enter-PSSession/Invoke-Command, Web mit Invoke-RestMethod
  • Provider machen auch Registry und Umgebung wie ein Dateisystem bedienbar

Übungsaufgabe

Lies eine Logdatei ein, zähle Meldungen pro Stufe und exportiere das Ergebnis als CSV.

Quiz zur Selbstkontrolle

Weiter im Kurs

Zurück: Funktionen, Parameter und Fehlerbehandlung

Weiter: Projekte: Praxisskripte

Alle Kapitel: PowerShell im Überblick