Zurück zu PowerShell
Module & Profile
PowerShell dauerhaft anpassen, Module installieren und eigene erstellen.
Das PowerShell-Profil ($PROFILE)
Das Profil ist ein Skript, das bei jedem Start automatisch ausgeführt wird.
# Profil-Pfad anzeigen
$PROFILE
# Profil erstellen (falls nicht vorhanden)
if (!(Test-Path $PROFILE)) { New-Item $PROFILE -Force }
# Profil bearbeiten
notepad $PROFILE
Nützliche Profil-Einträge
# Aliase setzen
Set-Alias -Name ll -Value Get-ChildItem
Set-Alias -Name np -Value notepad.exe
# Eigene Funktionen
function which ($command) { Get-Command $command | Select-Object Source }
function touch ($file) { if (!(Test-Path $file)) { New-Item $file } else { (Get-Item $file).LastWriteTime = Get-Date } }
# Begruessung
Write-Host "PowerShell bereit - $(Get-Date -Format 'dd.MM.yyyy HH:mm')" -ForegroundColor Cyan
Module verwalten
| Aktion | Befehl |
|---|---|
| Geladene Module | Get-Module |
| Verfügbare Module | Get-Module -ListAvailable |
| Modul suchen (Gallery) | Find-Module -Name *Active* |
| Modul installieren | Install-Module -Name PSReadLine -Scope CurrentUser |
| Modul laden | Import-Module ActiveDirectory |
| Modul aktualisieren | Update-Module -Name PSReadLine |
Eigenes Modul erstellen
# 1. Modulordner erstellen
$modulPfad = "$env:USERPROFILE\Documents\PowerShell\Modules\MeinModul"
New-Item $modulPfad -ItemType Directory -Force
# 2. Moduldatei (MeinModul.psm1)
@'
function Get-SystemReport {
[PSCustomObject]@{
Computer = $env:COMPUTERNAME
OS = (Get-CimInstance Win32_OperatingSystem).Caption
Uptime = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
FreiGB = [math]::Round((Get-PSDrive C).Free / 1GB, 1)
}
}
Export-ModuleMember -Function Get-SystemReport
'@ | Set-Content "$modulPfad\MeinModul.psm1"
# 3. Modul laden und nutzen
Import-Module MeinModul
Get-SystemReport
Tipp: Die PowerShell Gallery (powershellgallery.com) enthält tausende Community-Module. Beliebte Module: PSReadLine (bessere Eingabe), Terminal-Icons (Datei-Icons), Pester (Testing).