Advertisement
PiXLFAIL

Standard-Browser-Setzen

Jan 29th, 2025
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Lade Windows Forms für Dialoge
  2. Add-Type -AssemblyName System.Windows.Forms
  3.  
  4. # Pruefe auf Admin-Rechte
  5. $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
  6. $isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
  7.  
  8. if (-not $isAdmin) {
  9.     Write-Host "Dieses Skript muss als Administrator ausgefuehrt werden." -ForegroundColor Red
  10.     Write-Host "Bitte PowerShell als Administrator starten und das Skript erneut ausfuehren." -ForegroundColor Yellow
  11.     pause
  12.     exit 1
  13. }
  14.  
  15. # Funktion zum Anzeigen des Ordner-Dialogs
  16. function Show-FolderBrowserDialog {
  17.     param (
  18.         [string]$Description = "Bitte waehlen Sie einen Ordner"
  19.     )
  20.    
  21.     $folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
  22.     $folderBrowser.Description = $Description
  23.     $folderBrowser.ShowNewFolderButton = $true
  24.    
  25.     if ($folderBrowser.ShowDialog() -eq "OK") {
  26.         return $folderBrowser.SelectedPath
  27.     }
  28.     return $null
  29. }
  30.  
  31. # Funktion zum Anzeigen des Datei-Dialogs
  32. function Show-FileDialog {
  33.     param (
  34.         [string]$Title = "Datei auswählen",
  35.         [string]$Filter = "XML-Dateien (*.xml)|*.xml|Alle Dateien (*.*)|*.*",
  36.         [switch]$Save
  37.     )
  38.    
  39.     if ($Save) {
  40.         $fileDialog = New-Object System.Windows.Forms.SaveFileDialog
  41.     } else {
  42.         $fileDialog = New-Object System.Windows.Forms.OpenFileDialog
  43.     }
  44.    
  45.     $fileDialog.Title = $Title
  46.     $fileDialog.Filter = $Filter
  47.     $fileDialog.DefaultExt = "xml"
  48.    
  49.     if ($fileDialog.ShowDialog() -eq "OK") {
  50.         return $fileDialog.FileName
  51.     }
  52.     return $null
  53. }
  54.  
  55. # Funktion zum Abrufen der aktuellen Browser-Einstellungen
  56. function Get-CurrentBrowserSettings {
  57.     $httpHandler = (Get-ItemProperty "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" -ErrorAction SilentlyContinue).ProgId
  58.     $httpsHandler = (Get-ItemProperty "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice" -ErrorAction SilentlyContinue).ProgId
  59.    
  60.     Write-Host "`nAktuelle Browser-Einstellungen:" -ForegroundColor Cyan
  61.     Write-Host "HTTP Handler: $httpHandler" -ForegroundColor Gray
  62.     Write-Host "HTTPS Handler: $httpsHandler" -ForegroundColor Gray
  63. }
  64.  
  65. # Funktion zum Erkennen installierter Browser
  66. function Get-InstalledBrowsers {
  67.     $browsers = @()
  68.    
  69.     # Firefox
  70.     $firefoxPath = $null
  71.     if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe") {
  72.         $firefoxPath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe")."(Default)"
  73.     }
  74.     if (-not $firefoxPath -or -not (Test-Path $firefoxPath)) {
  75.         Write-Host "Firefox-Installation konnte nicht automatisch gefunden werden." -ForegroundColor Yellow
  76.         Write-Host "Moechten Sie den Firefox-Installationsordner manuell auswaehlen? (J/N)" -ForegroundColor Cyan
  77.         $response = Read-Host
  78.         if ($response -eq "J") {
  79.             $selectedPath = Show-FolderBrowserDialog "Bitte waehlen Sie den Firefox-Installationsordner"
  80.             if ($selectedPath) {
  81.                 $firefoxPath = Join-Path $selectedPath "firefox.exe"
  82.                 if (Test-Path $firefoxPath) {
  83.                     Write-Host "Firefox.exe gefunden in: $firefoxPath" -ForegroundColor Green
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     if ($firefoxPath -and (Test-Path $firefoxPath)) {
  89.         $browsers += @{
  90.             Name = "Firefox"
  91.             ProgID = "FirefoxURL"
  92.             Path = $firefoxPath
  93.             Command = "firefox.exe"
  94.             Args = "-setDefaultBrowser"
  95.         }
  96.     }
  97.    
  98.     # Chrome
  99.     $chromePath = $null
  100.     if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe") {
  101.         $chromePath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe")."(Default)"
  102.     }
  103.     if (-not $chromePath -or -not (Test-Path $chromePath)) {
  104.         Write-Host "Chrome-Installation konnte nicht automatisch gefunden werden." -ForegroundColor Yellow
  105.         Write-Host "Moechten Sie den Chrome-Installationsordner manuell auswaehlen? (J/N)" -ForegroundColor Cyan
  106.         $response = Read-Host
  107.         if ($response -eq "J") {
  108.             $selectedPath = Show-FolderBrowserDialog "Bitte waehlen Sie den Chrome-Installationsordner"
  109.             if ($selectedPath) {
  110.                 $chromePath = Join-Path $selectedPath "chrome.exe"
  111.                 if (Test-Path $chromePath) {
  112.                     Write-Host "Chrome.exe gefunden in: $chromePath" -ForegroundColor Green
  113.                 }
  114.             }
  115.         }
  116.     }
  117.     if ($chromePath -and (Test-Path $chromePath)) {
  118.         $browsers += @{
  119.             Name = "Chrome"
  120.             ProgID = "ChromeHTML"
  121.             Path = $chromePath
  122.             Command = "chrome.exe"
  123.             Args = "--make-default-browser"
  124.         }
  125.     }
  126.    
  127.     # Edge
  128.     $edgePath = $null
  129.     if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe") {
  130.         $edgePath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe")."(Default)"
  131.     }
  132.     if (-not $edgePath -or -not (Test-Path $edgePath)) {
  133.         Write-Host "Edge-Installation konnte nicht automatisch gefunden werden." -ForegroundColor Yellow
  134.         Write-Host "Moechten Sie den Edge-Installationsordner manuell auswaehlen? (J/N)" -ForegroundColor Cyan
  135.         $response = Read-Host
  136.         if ($response -eq "J") {
  137.             $selectedPath = Show-FolderBrowserDialog "Bitte waehlen Sie den Edge-Installationsordner"
  138.             if ($selectedPath) {
  139.                 $edgePath = Join-Path $selectedPath "msedge.exe"
  140.                 if (Test-Path $edgePath) {
  141.                     Write-Host "MSEdge.exe gefunden in: $edgePath" -ForegroundColor Green
  142.                 }
  143.             }
  144.         }
  145.     }
  146.     if ($edgePath -and (Test-Path $edgePath)) {
  147.         $browsers += @{
  148.             Name = "Microsoft Edge"
  149.             ProgID = "MSEdgeHTM"
  150.             Path = $edgePath
  151.             Command = "msedge.exe"
  152.             Args = "--make-default-browser"
  153.         }
  154.     }
  155.    
  156.     return $browsers
  157. }
  158.  
  159. # Funktion zum Setzen des Standard-Browsers
  160. function Set-DefaultBrowser {
  161.     param (
  162.         [string]$BrowserCommand,
  163.         [string]$BrowserPath,
  164.         [string]$Args
  165.     )
  166.    
  167.     try {
  168.         # Verwende den Browser-eigenen Befehl zum Setzen als Standard
  169.         $browserDir = Split-Path -Parent $BrowserPath
  170.         Push-Location $browserDir
  171.        
  172.         Write-Host "Führe aus: $BrowserPath $Args" -ForegroundColor Yellow
  173.        
  174.         # Versuche den Befehl auszuführen
  175.         if ($Args) {
  176.             $process = Start-Process -FilePath $BrowserPath -ArgumentList $Args -Wait -PassThru
  177.         } else {
  178.             # Fallback für Firefox: Verwende -setDefaultBrowser wenn keine Argumente angegeben sind
  179.             $process = Start-Process -FilePath $BrowserPath -ArgumentList "-setDefaultBrowser" -Wait -PassThru
  180.         }
  181.        
  182.         # Wenn der Befehl fehlschlägt, versuche alternative Methoden
  183.         if ($process.ExitCode -ne 0) {
  184.             Write-Host "Browser-Befehl fehlgeschlagen. Versuche alternative Methode..." -ForegroundColor Yellow
  185.            
  186.             # Versuche alternative Befehle basierend auf dem Browser
  187.             if ($BrowserPath -like "*firefox*") {
  188.                 Start-Process -FilePath $BrowserPath -ArgumentList "--setDefaultBrowser" -Wait
  189.             }
  190.             elseif ($BrowserPath -like "*chrome*") {
  191.                 Start-Process -FilePath $BrowserPath -ArgumentList "--make-default-browser" -Wait
  192.             }
  193.             elseif ($BrowserPath -like "*msedge*") {
  194.                 Start-Process -FilePath $BrowserPath -ArgumentList "--make-default-browser" -Wait
  195.             }
  196.         }
  197.        
  198.         Pop-Location
  199.         return $true
  200.     }
  201.     catch {
  202.         Write-Host "Fehler beim Setzen des Standard-Browsers: $_" -ForegroundColor Red
  203.         return $false
  204.     }
  205. }
  206.  
  207. # Funktion zum Exportieren der Browser-Einstellungen
  208. function Export-BrowserSettings {
  209.     try {
  210.         # Öffne Speichern-Dialog
  211.         $exportPath = Show-FileDialog -Title "Browser-Einstellungen speichern" -Save
  212.         if (-not $exportPath) { return $false }
  213.        
  214.         # Exportiere direkt in die Zieldatei
  215.         Write-Host "Exportiere aktuelle Browser-Einstellungen..." -ForegroundColor Yellow
  216.        
  217.         # Verwende den vollständigen Pfad für DISM
  218.         $fullPath = [System.IO.Path]::GetFullPath($exportPath)
  219.         Start-Process -FilePath "dism.exe" -ArgumentList "/online /Export-DefaultAppAssociations:`"$fullPath`"" -Wait -NoNewWindow
  220.        
  221.         if (Test-Path $fullPath) {
  222.             Write-Host "Browser-Einstellungen wurden exportiert nach: $fullPath" -ForegroundColor Green
  223.             return $true
  224.         }
  225.        
  226.         Write-Host "Export-Datei wurde nicht erstellt." -ForegroundColor Red
  227.         return $false
  228.     }
  229.     catch {
  230.         Write-Host "Fehler beim Exportieren der Browser-Einstellungen: $_" -ForegroundColor Red
  231.         return $false
  232.     }
  233. }
  234.  
  235. # Funktion zum Importieren der Browser-Einstellungen
  236. function Import-BrowserSettings {
  237.     # Öffne Öffnen-Dialog
  238.     $importPath = Show-FileDialog -Title "Browser-Einstellungen laden"
  239.     if (-not $importPath) { return $false }
  240.    
  241.     if (-not (Test-Path $importPath)) {
  242.         Write-Host "Die angegebene Konfigurationsdatei wurde nicht gefunden: $importPath" -ForegroundColor Red
  243.         return $false
  244.     }
  245.    
  246.     try {
  247.         Write-Host "Importiere Browser-Einstellungen..." -ForegroundColor Yellow
  248.         Start-Process -FilePath "dism.exe" -ArgumentList "/online /Import-DefaultAppAssociations:`"$importPath`"" -Wait -NoNewWindow
  249.         Write-Host "Browser-Einstellungen wurden erfolgreich importiert." -ForegroundColor Green
  250.         return $true
  251.     }
  252.     catch {
  253.         Write-Host "Fehler beim Importieren der Browser-Einstellungen: $_" -ForegroundColor Red
  254.         return $false
  255.     }
  256. }
  257.  
  258. # Hauptprogramm
  259. Clear-Host
  260. Write-Host "Standard-Browser Konfiguration" -ForegroundColor Cyan
  261. Write-Host "=============================" -ForegroundColor Cyan
  262.  
  263. # Zeige Hauptmenü
  264. Write-Host "`nWas moechten Sie tun?" -ForegroundColor Cyan
  265. Write-Host "[1] Browser manuell als Standard festlegen" -ForegroundColor Yellow
  266. Write-Host "[2] Aktuelle Browser-Einstellungen exportieren" -ForegroundColor Yellow
  267. Write-Host "[3] Browser-Einstellungen aus Datei importieren" -ForegroundColor Yellow
  268. Write-Host "[4] Aktuelle Einstellungen anzeigen" -ForegroundColor Yellow
  269. Write-Host "[5] Beenden" -ForegroundColor Yellow
  270.  
  271. do {
  272.     $choice = Read-Host "`nIhre Wahl"
  273.    
  274.     switch ($choice) {
  275.         "1" {
  276.             # Zeige aktuelle Einstellungen
  277.             Get-CurrentBrowserSettings
  278.            
  279.             # Erkenne installierte Browser
  280.             $browsers = Get-InstalledBrowsers
  281.            
  282.             if ($browsers.Count -eq 0) {
  283.                 Write-Host "`nKeine Browser gefunden!" -ForegroundColor Red
  284.                 Write-Host "Bitte stellen Sie sicher, dass mindestens ein Browser installiert ist." -ForegroundColor Yellow
  285.                 pause
  286.                 break
  287.             }
  288.            
  289.             # Zeige verfuegbare Browser
  290.             Write-Host "`nVerfuegbare Browser:" -ForegroundColor Cyan
  291.             for ($i = 0; $i -lt $browsers.Count; $i++) {
  292.                 Write-Host "[$($i + 1)] $($browsers[$i].Name)" -ForegroundColor Yellow
  293.                 Write-Host "    Pfad: $($browsers[$i].Path)" -ForegroundColor Gray
  294.             }
  295.            
  296.             # Benutzerauswahl
  297.             do {
  298.                 Write-Host "`nWelchen Browser moechten Sie als Standard festlegen? (1-$($browsers.Count))" -ForegroundColor Cyan
  299.                 $browserChoice = Read-Host "Ihre Wahl"
  300.                
  301.                 if ($browserChoice -match '^\d+$' -and [int]$browserChoice -ge 1 -and [int]$browserChoice -le $browsers.Count) {
  302.                     $selectedBrowser = $browsers[[int]$browserChoice - 1]
  303.                     break
  304.                 } else {
  305.                     Write-Host "Ungueltige Eingabe. Bitte waehlen Sie eine Zahl zwischen 1 und $($browsers.Count)." -ForegroundColor Red
  306.                 }
  307.             } while ($true)
  308.            
  309.             # Setze ausgewaehlten Browser als Standard
  310.             Write-Host "`nSetze $($selectedBrowser.Name) als Standard-Browser..." -ForegroundColor Cyan
  311.             $success = Set-DefaultBrowser -BrowserCommand $selectedBrowser.Command -BrowserPath $selectedBrowser.Path -Args $selectedBrowser.Args
  312.            
  313.             if ($success) {
  314.                 Write-Host "`n$($selectedBrowser.Name) wurde als Standard-Browser festgelegt." -ForegroundColor Green
  315.                 Write-Host "Bitte starten Sie den Computer neu, damit alle Aenderungen wirksam werden." -ForegroundColor Yellow
  316.             } else {
  317.                 Write-Host "`nFehler beim Setzen des Standard-Browsers." -ForegroundColor Red
  318.                 Write-Host "Bitte versuchen Sie die Einstellung ueber die Windows-Einstellungen zu aendern:" -ForegroundColor Yellow
  319.                 Write-Host "Windows-Einstellungen > Apps > Standard-Apps > Webbrowser" -ForegroundColor Yellow
  320.             }
  321.             pause
  322.             break
  323.         }
  324.         "2" {
  325.             Export-BrowserSettings
  326.             pause
  327.             break
  328.         }
  329.         "3" {
  330.             Import-BrowserSettings
  331.             pause
  332.             break
  333.         }
  334.         "4" {
  335.             Get-CurrentBrowserSettings
  336.             pause
  337.             break
  338.         }
  339.         "5" {
  340.             exit
  341.         }
  342.         default {
  343.             Write-Host "Ungueltige Eingabe. Bitte waehlen Sie eine Zahl zwischen 1 und 5." -ForegroundColor Red
  344.         }
  345.     }
  346. } while ($true)
  347.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement