# Ensure admin privileges $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { exit } $wgExe = "C:\Program Files\WireGuard\wireguard.exe" $dataPath = "C:\Program Files\WireGuard\Data" # 1. Stop WireGuard service Write-Host "Stopping WireGuard service..." Stop-Service -Name "WireGuardManager" -Force -ErrorAction SilentlyContinue Get-Service -Name "WireGuardTunnel*" | Stop-Service -Force -ErrorAction SilentlyContinue # 2. Remove WireGuard network adapters Write-Host "Removing WireGuard network adapters..." # Disable adapters first Get-NetAdapter -Name "WireGuard*" -ErrorAction SilentlyContinue | Disable-NetAdapter -Confirm:$false -ErrorAction SilentlyContinue # Use netsh to delete WireGuard interfaces $adapters = Get-NetAdapter -Name "WireGuard*" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Name foreach ($adapter in $adapters) { & netsh interface delete interface "$adapter" | Out-Null } # 3. Clean up WireGuard driver files Write-Host "Cleaning up WireGuard driver files..." $driverPath = "$dataPath\Configurations" if (Test-Path $driverPath) { Remove-Item -Path "$driverPath\*.log" -Force -ErrorAction SilentlyContinue } # 4. Start WireGuard service Write-Host "Starting WireGuard service..." Start-Service -Name "WireGuardManager" -ErrorAction SilentlyContinue # 5. Reinitialize WireGuard tunnels Write-Host "Reinitializing WireGuard tunnels..." Get-ChildItem -Path "$dataPath\Configurations" -Filter "*.conf" -ErrorAction SilentlyContinue | ForEach-Object { Start-Process -FilePath $wgExe -ArgumentList "/installtunnelservice `"$($_.FullName)`"" -NoNewWindow -ErrorAction SilentlyContinue }