Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Lade Windows Forms für Dialoge
- Add-Type -AssemblyName System.Windows.Forms
- # Pruefe auf Admin-Rechte
- $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
- $isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
- if (-not $isAdmin) {
- Write-Host "Dieses Skript muss als Administrator ausgefuehrt werden." -ForegroundColor Red
- Write-Host "Bitte PowerShell als Administrator starten und das Skript erneut ausfuehren." -ForegroundColor Yellow
- pause
- exit 1
- }
- # Funktion zum Anzeigen des Ordner-Dialogs
- function Show-FolderBrowserDialog {
- param (
- [string]$Description = "Bitte waehlen Sie einen Ordner"
- )
- $folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
- $folderBrowser.Description = $Description
- $folderBrowser.ShowNewFolderButton = $true
- if ($folderBrowser.ShowDialog() -eq "OK") {
- return $folderBrowser.SelectedPath
- }
- return $null
- }
- # Funktion zum Anzeigen des Datei-Dialogs
- function Show-FileDialog {
- param (
- [string]$Title = "Datei auswählen",
- [string]$Filter = "XML-Dateien (*.xml)|*.xml|Alle Dateien (*.*)|*.*",
- [switch]$Save
- )
- if ($Save) {
- $fileDialog = New-Object System.Windows.Forms.SaveFileDialog
- } else {
- $fileDialog = New-Object System.Windows.Forms.OpenFileDialog
- }
- $fileDialog.Title = $Title
- $fileDialog.Filter = $Filter
- $fileDialog.DefaultExt = "xml"
- if ($fileDialog.ShowDialog() -eq "OK") {
- return $fileDialog.FileName
- }
- return $null
- }
- # Funktion zum Abrufen der aktuellen Browser-Einstellungen
- function Get-CurrentBrowserSettings {
- $httpHandler = (Get-ItemProperty "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" -ErrorAction SilentlyContinue).ProgId
- $httpsHandler = (Get-ItemProperty "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice" -ErrorAction SilentlyContinue).ProgId
- Write-Host "`nAktuelle Browser-Einstellungen:" -ForegroundColor Cyan
- Write-Host "HTTP Handler: $httpHandler" -ForegroundColor Gray
- Write-Host "HTTPS Handler: $httpsHandler" -ForegroundColor Gray
- }
- # Funktion zum Erkennen installierter Browser
- function Get-InstalledBrowsers {
- $browsers = @()
- # Firefox
- $firefoxPath = $null
- if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe") {
- $firefoxPath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe")."(Default)"
- }
- if (-not $firefoxPath -or -not (Test-Path $firefoxPath)) {
- Write-Host "Firefox-Installation konnte nicht automatisch gefunden werden." -ForegroundColor Yellow
- Write-Host "Moechten Sie den Firefox-Installationsordner manuell auswaehlen? (J/N)" -ForegroundColor Cyan
- $response = Read-Host
- if ($response -eq "J") {
- $selectedPath = Show-FolderBrowserDialog "Bitte waehlen Sie den Firefox-Installationsordner"
- if ($selectedPath) {
- $firefoxPath = Join-Path $selectedPath "firefox.exe"
- if (Test-Path $firefoxPath) {
- Write-Host "Firefox.exe gefunden in: $firefoxPath" -ForegroundColor Green
- }
- }
- }
- }
- if ($firefoxPath -and (Test-Path $firefoxPath)) {
- $browsers += @{
- Name = "Firefox"
- ProgID = "FirefoxURL"
- Path = $firefoxPath
- Command = "firefox.exe"
- Args = "-setDefaultBrowser"
- }
- }
- # Chrome
- $chromePath = $null
- if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe") {
- $chromePath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe")."(Default)"
- }
- if (-not $chromePath -or -not (Test-Path $chromePath)) {
- Write-Host "Chrome-Installation konnte nicht automatisch gefunden werden." -ForegroundColor Yellow
- Write-Host "Moechten Sie den Chrome-Installationsordner manuell auswaehlen? (J/N)" -ForegroundColor Cyan
- $response = Read-Host
- if ($response -eq "J") {
- $selectedPath = Show-FolderBrowserDialog "Bitte waehlen Sie den Chrome-Installationsordner"
- if ($selectedPath) {
- $chromePath = Join-Path $selectedPath "chrome.exe"
- if (Test-Path $chromePath) {
- Write-Host "Chrome.exe gefunden in: $chromePath" -ForegroundColor Green
- }
- }
- }
- }
- if ($chromePath -and (Test-Path $chromePath)) {
- $browsers += @{
- Name = "Chrome"
- ProgID = "ChromeHTML"
- Path = $chromePath
- Command = "chrome.exe"
- Args = "--make-default-browser"
- }
- }
- # Edge
- $edgePath = $null
- if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe") {
- $edgePath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe")."(Default)"
- }
- if (-not $edgePath -or -not (Test-Path $edgePath)) {
- Write-Host "Edge-Installation konnte nicht automatisch gefunden werden." -ForegroundColor Yellow
- Write-Host "Moechten Sie den Edge-Installationsordner manuell auswaehlen? (J/N)" -ForegroundColor Cyan
- $response = Read-Host
- if ($response -eq "J") {
- $selectedPath = Show-FolderBrowserDialog "Bitte waehlen Sie den Edge-Installationsordner"
- if ($selectedPath) {
- $edgePath = Join-Path $selectedPath "msedge.exe"
- if (Test-Path $edgePath) {
- Write-Host "MSEdge.exe gefunden in: $edgePath" -ForegroundColor Green
- }
- }
- }
- }
- if ($edgePath -and (Test-Path $edgePath)) {
- $browsers += @{
- Name = "Microsoft Edge"
- ProgID = "MSEdgeHTM"
- Path = $edgePath
- Command = "msedge.exe"
- Args = "--make-default-browser"
- }
- }
- return $browsers
- }
- # Funktion zum Setzen des Standard-Browsers
- function Set-DefaultBrowser {
- param (
- [string]$BrowserCommand,
- [string]$BrowserPath,
- [string]$Args
- )
- try {
- # Verwende den Browser-eigenen Befehl zum Setzen als Standard
- $browserDir = Split-Path -Parent $BrowserPath
- Push-Location $browserDir
- Write-Host "Führe aus: $BrowserPath $Args" -ForegroundColor Yellow
- # Versuche den Befehl auszuführen
- if ($Args) {
- $process = Start-Process -FilePath $BrowserPath -ArgumentList $Args -Wait -PassThru
- } else {
- # Fallback für Firefox: Verwende -setDefaultBrowser wenn keine Argumente angegeben sind
- $process = Start-Process -FilePath $BrowserPath -ArgumentList "-setDefaultBrowser" -Wait -PassThru
- }
- # Wenn der Befehl fehlschlägt, versuche alternative Methoden
- if ($process.ExitCode -ne 0) {
- Write-Host "Browser-Befehl fehlgeschlagen. Versuche alternative Methode..." -ForegroundColor Yellow
- # Versuche alternative Befehle basierend auf dem Browser
- if ($BrowserPath -like "*firefox*") {
- Start-Process -FilePath $BrowserPath -ArgumentList "--setDefaultBrowser" -Wait
- }
- elseif ($BrowserPath -like "*chrome*") {
- Start-Process -FilePath $BrowserPath -ArgumentList "--make-default-browser" -Wait
- }
- elseif ($BrowserPath -like "*msedge*") {
- Start-Process -FilePath $BrowserPath -ArgumentList "--make-default-browser" -Wait
- }
- }
- Pop-Location
- return $true
- }
- catch {
- Write-Host "Fehler beim Setzen des Standard-Browsers: $_" -ForegroundColor Red
- return $false
- }
- }
- # Funktion zum Exportieren der Browser-Einstellungen
- function Export-BrowserSettings {
- try {
- # Öffne Speichern-Dialog
- $exportPath = Show-FileDialog -Title "Browser-Einstellungen speichern" -Save
- if (-not $exportPath) { return $false }
- # Exportiere direkt in die Zieldatei
- Write-Host "Exportiere aktuelle Browser-Einstellungen..." -ForegroundColor Yellow
- # Verwende den vollständigen Pfad für DISM
- $fullPath = [System.IO.Path]::GetFullPath($exportPath)
- Start-Process -FilePath "dism.exe" -ArgumentList "/online /Export-DefaultAppAssociations:`"$fullPath`"" -Wait -NoNewWindow
- if (Test-Path $fullPath) {
- Write-Host "Browser-Einstellungen wurden exportiert nach: $fullPath" -ForegroundColor Green
- return $true
- }
- Write-Host "Export-Datei wurde nicht erstellt." -ForegroundColor Red
- return $false
- }
- catch {
- Write-Host "Fehler beim Exportieren der Browser-Einstellungen: $_" -ForegroundColor Red
- return $false
- }
- }
- # Funktion zum Importieren der Browser-Einstellungen
- function Import-BrowserSettings {
- # Öffne Öffnen-Dialog
- $importPath = Show-FileDialog -Title "Browser-Einstellungen laden"
- if (-not $importPath) { return $false }
- if (-not (Test-Path $importPath)) {
- Write-Host "Die angegebene Konfigurationsdatei wurde nicht gefunden: $importPath" -ForegroundColor Red
- return $false
- }
- try {
- Write-Host "Importiere Browser-Einstellungen..." -ForegroundColor Yellow
- Start-Process -FilePath "dism.exe" -ArgumentList "/online /Import-DefaultAppAssociations:`"$importPath`"" -Wait -NoNewWindow
- Write-Host "Browser-Einstellungen wurden erfolgreich importiert." -ForegroundColor Green
- return $true
- }
- catch {
- Write-Host "Fehler beim Importieren der Browser-Einstellungen: $_" -ForegroundColor Red
- return $false
- }
- }
- # Hauptprogramm
- Clear-Host
- Write-Host "Standard-Browser Konfiguration" -ForegroundColor Cyan
- Write-Host "=============================" -ForegroundColor Cyan
- # Zeige Hauptmenü
- Write-Host "`nWas moechten Sie tun?" -ForegroundColor Cyan
- Write-Host "[1] Browser manuell als Standard festlegen" -ForegroundColor Yellow
- Write-Host "[2] Aktuelle Browser-Einstellungen exportieren" -ForegroundColor Yellow
- Write-Host "[3] Browser-Einstellungen aus Datei importieren" -ForegroundColor Yellow
- Write-Host "[4] Aktuelle Einstellungen anzeigen" -ForegroundColor Yellow
- Write-Host "[5] Beenden" -ForegroundColor Yellow
- do {
- $choice = Read-Host "`nIhre Wahl"
- switch ($choice) {
- "1" {
- # Zeige aktuelle Einstellungen
- Get-CurrentBrowserSettings
- # Erkenne installierte Browser
- $browsers = Get-InstalledBrowsers
- if ($browsers.Count -eq 0) {
- Write-Host "`nKeine Browser gefunden!" -ForegroundColor Red
- Write-Host "Bitte stellen Sie sicher, dass mindestens ein Browser installiert ist." -ForegroundColor Yellow
- pause
- break
- }
- # Zeige verfuegbare Browser
- Write-Host "`nVerfuegbare Browser:" -ForegroundColor Cyan
- for ($i = 0; $i -lt $browsers.Count; $i++) {
- Write-Host "[$($i + 1)] $($browsers[$i].Name)" -ForegroundColor Yellow
- Write-Host " Pfad: $($browsers[$i].Path)" -ForegroundColor Gray
- }
- # Benutzerauswahl
- do {
- Write-Host "`nWelchen Browser moechten Sie als Standard festlegen? (1-$($browsers.Count))" -ForegroundColor Cyan
- $browserChoice = Read-Host "Ihre Wahl"
- if ($browserChoice -match '^\d+$' -and [int]$browserChoice -ge 1 -and [int]$browserChoice -le $browsers.Count) {
- $selectedBrowser = $browsers[[int]$browserChoice - 1]
- break
- } else {
- Write-Host "Ungueltige Eingabe. Bitte waehlen Sie eine Zahl zwischen 1 und $($browsers.Count)." -ForegroundColor Red
- }
- } while ($true)
- # Setze ausgewaehlten Browser als Standard
- Write-Host "`nSetze $($selectedBrowser.Name) als Standard-Browser..." -ForegroundColor Cyan
- $success = Set-DefaultBrowser -BrowserCommand $selectedBrowser.Command -BrowserPath $selectedBrowser.Path -Args $selectedBrowser.Args
- if ($success) {
- Write-Host "`n$($selectedBrowser.Name) wurde als Standard-Browser festgelegt." -ForegroundColor Green
- Write-Host "Bitte starten Sie den Computer neu, damit alle Aenderungen wirksam werden." -ForegroundColor Yellow
- } else {
- Write-Host "`nFehler beim Setzen des Standard-Browsers." -ForegroundColor Red
- Write-Host "Bitte versuchen Sie die Einstellung ueber die Windows-Einstellungen zu aendern:" -ForegroundColor Yellow
- Write-Host "Windows-Einstellungen > Apps > Standard-Apps > Webbrowser" -ForegroundColor Yellow
- }
- pause
- break
- }
- "2" {
- Export-BrowserSettings
- pause
- break
- }
- "3" {
- Import-BrowserSettings
- pause
- break
- }
- "4" {
- Get-CurrentBrowserSettings
- pause
- break
- }
- "5" {
- exit
- }
- default {
- Write-Host "Ungueltige Eingabe. Bitte waehlen Sie eine Zahl zwischen 1 und 5." -ForegroundColor Red
- }
- }
- } while ($true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement