@echo off setlocal set "PS1=%TEMP%\Install-Ethernet-Toggle-%RANDOM%.ps1" :: ------------------------------------------------------------ :: Extract embedded PowerShell installer :: ------------------------------------------------------------ powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^ "$lines = Get-Content -LiteralPath '%~f0'; ^ $i = [Array]::IndexOf($lines, '###POWERSHELL###'); ^ if ($i -lt 0) { exit 1 }; ^ [System.IO.File]::WriteAllLines('%PS1%', $lines[($i+1)..($lines.Length-1)], (New-Object System.Text.UTF8Encoding($false)))" if errorlevel 1 ( echo Failed to extract installer. pause exit /b 1 ) :: ------------------------------------------------------------ :: Run installer :: ------------------------------------------------------------ powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%PS1%" :: ------------------------------------------------------------ :: Cleanup temporary installer :: ------------------------------------------------------------ del "%PS1%" >nul 2>&1 exit /b ###POWERSHELL### # ============================================================ # Ethernet Toggle Installer # ============================================================ $ErrorActionPreference = "Stop" # ------------------------------------------------------------ # Self-elevate # ------------------------------------------------------------ $IsAdmin = ( New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent() ) ).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) if (-not $IsAdmin) { Start-Process ` powershell.exe ` -Verb RunAs ` -Wait ` -ArgumentList @( "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "`"$PSCommandPath`"" ) exit } # ------------------------------------------------------------ # Configuration # ------------------------------------------------------------ $AppName = "Ethernet Toggle" $Version = "1.0" $Publisher = "Ethernet Toggle Utility" $InstallDir = Join-Path $env:ProgramData "EthernetToggle" $ToggleScript = Join-Path $InstallDir "ToggleEthernet.ps1" $Launcher = Join-Path $InstallDir "ToggleEthernet.vbs" $Uninstaller = Join-Path $InstallDir "Uninstall-EthernetToggle.ps1" $TaskName = "Ethernet Toggle" $RegPath = ` "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\EthernetToggle" New-Item ` -ItemType Directory ` -Path $InstallDir ` -Force | Out-Null # ============================================================ # 1. Ethernet toggle script # ============================================================ $ToggleCode = @' Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing [System.Windows.Forms.Application]::EnableVisualStyles() # ------------------------------------------------------------ # UI language: Turkish / German / English fallback # ------------------------------------------------------------ $lang = [System.Globalization.CultureInfo]::CurrentUICulture.TwoLetterISOLanguageName if ($lang -eq 'tr') { # Build Turkish characters at runtime so the BAT/PS1 source stays ASCII-safe $cced = [char]231 $uuml = [char]252 $gbre = [char]287 $sced = [char]351 $idot = [char]305 $Cced = [char]199 $title = "Ethernet A${cced}/Kapat" $waitTitle = "L${uuml}tfen bekleyin..." $waitText = "Ethernet ba${gbre}da${sced}t${idot}r${idot}c${idot}s${idot} de${gbre}i${sced}tiriliyor." $onText = "art${idot}k A${Cced}IK." $offText = "art${idot}k KAPALI." $notFound = "Fiziksel Ethernet ba${gbre}da${sced}t${idot}r${idot}c${idot}s${idot} bulunamad${idot}." } else { $title = 'Ethernet Toggle' $waitTitle = 'Please wait...' $waitText = 'Switching Ethernet adapter.' $onText = 'is now ON.' $offText = 'is now OFF.' $notFound = 'No physical Ethernet adapter was found.' } # ------------------------------------------------------------ # Detect Ethernet adapter # ------------------------------------------------------------ $adapters = @(Get-NetAdapter -Physical -ErrorAction SilentlyContinue) $eth = $null # Prefer onboard / non-USB physical Ethernet adapter foreach ($a in $adapters) { if ( $a.MediaType -eq '802.3' -and $a.InterfaceDescription -notmatch 'USB' ) { $eth = $a break } } # Fall back to any physical Ethernet adapter if (-not $eth) { foreach ($a in $adapters) { if ($a.MediaType -eq '802.3') { $eth = $a break } } } if (-not $eth) { [System.Windows.Forms.MessageBox]::Show( $notFound, $title, [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning ) | Out-Null exit } # ------------------------------------------------------------ # Waiting window # ------------------------------------------------------------ $form = New-Object System.Windows.Forms.Form $form.Text = $title $form.StartPosition = 'CenterScreen' $form.FormBorderStyle = 'FixedDialog' $form.MaximizeBox = $false $form.MinimizeBox = $false $form.ShowInTaskbar = $true $form.TopMost = $true $form.ClientSize = New-Object System.Drawing.Size(390, 115) $label1 = New-Object System.Windows.Forms.Label $label1.AutoSize = $true $label1.Font = New-Object System.Drawing.Font( 'Segoe UI', 11, [System.Drawing.FontStyle]::Bold ) $label1.Location = New-Object System.Drawing.Point(18, 14) $label1.Text = $waitTitle $label2 = New-Object System.Windows.Forms.Label $label2.AutoSize = $true $label2.Font = New-Object System.Drawing.Font('Segoe UI', 9) $label2.Location = New-Object System.Drawing.Point(18, 43) $label2.Text = $waitText $progress = New-Object System.Windows.Forms.ProgressBar $progress.Style = 'Marquee' $progress.MarqueeAnimationSpeed = 25 $progress.Location = New-Object System.Drawing.Point(20, 76) $progress.Size = New-Object System.Drawing.Size(350, 18) $form.Controls.Add($label1) $form.Controls.Add($label2) $form.Controls.Add($progress) $form.Show() $form.Activate() [System.Windows.Forms.Application]::DoEvents() $started = [System.Diagnostics.Stopwatch]::StartNew() # ------------------------------------------------------------ # Toggle Ethernet # ------------------------------------------------------------ if ($eth.Status -eq 'Disabled') { Enable-NetAdapter ` -InterfaceDescription $eth.InterfaceDescription ` -Confirm:$false ` -ErrorAction SilentlyContinue $stateText = $onText } else { Disable-NetAdapter ` -InterfaceDescription $eth.InterfaceDescription ` -Confirm:$false ` -ErrorAction SilentlyContinue $stateText = $offText } # Keep wait window visible briefly so it does not only flash while ($started.ElapsedMilliseconds -lt 650) { [System.Windows.Forms.Application]::DoEvents() Start-Sleep -Milliseconds 50 } $form.Close() $form.Dispose() # ------------------------------------------------------------ # Finished message # ------------------------------------------------------------ $adapterDisplay = $eth.InterfaceDescription if ($eth.Name -and $eth.Name -ne $eth.InterfaceDescription) { $adapterDisplay += " ($($eth.Name))" } [System.Windows.Forms.MessageBox]::Show( "$adapterDisplay $stateText", $title, [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information ) | Out-Null '@ Set-Content ` -LiteralPath $ToggleScript ` -Value $ToggleCode ` -Encoding UTF8 ` -Force # ============================================================ # 2. Scheduled task # ============================================================ $Action = New-ScheduledTaskAction ` -Execute "powershell.exe" ` -Argument ( "-NoProfile " + "-NonInteractive " + "-ExecutionPolicy Bypass " + "-WindowStyle Hidden " + "-File `"$ToggleScript`"" ) $CurrentUser = ` [Security.Principal.WindowsIdentity]::GetCurrent().Name $Principal = New-ScheduledTaskPrincipal ` -UserId $CurrentUser ` -LogonType Interactive ` -RunLevel Highest $Settings = New-ScheduledTaskSettingsSet ` -AllowStartIfOnBatteries ` -DontStopIfGoingOnBatteries ` -ExecutionTimeLimit (New-TimeSpan -Minutes 1) Register-ScheduledTask ` -TaskName $TaskName ` -Action $Action ` -Principal $Principal ` -Settings $Settings ` -Force | Out-Null # ============================================================ # 3. Invisible launcher # ============================================================ $VbsCode = @' Set WshShell = CreateObject("WScript.Shell") WshShell.Run "schtasks.exe /Run /TN ""Ethernet Toggle""", 0, False '@ Set-Content ` -LiteralPath $Launcher ` -Value $VbsCode ` -Encoding ASCII ` -Force # ============================================================ # 4. Desktop shortcut # ============================================================ $Desktop = [Environment]::GetFolderPath("Desktop") # Localize desktop shortcut name: # Turkish Windows UI -> "Ethernet Ac-Kapa" with the real c-cedilla character. # All other UI languages -> "Ethernet Toggle". $lang = [System.Globalization.CultureInfo]::CurrentUICulture.TwoLetterISOLanguageName $cced = [char]231 $TurkishShortcutName = "Ethernet A${cced}-Kapa.lnk" $EnglishShortcutName = "Ethernet Toggle.lnk" if ($lang -eq "tr") { $ShortcutName = $TurkishShortcutName } else { $ShortcutName = $EnglishShortcutName } # Remove an older shortcut name if this is a reinstall or UI language changed. Remove-Item ` (Join-Path $Desktop $TurkishShortcutName) ` -Force ` -ErrorAction SilentlyContinue Remove-Item ` (Join-Path $Desktop $EnglishShortcutName) ` -Force ` -ErrorAction SilentlyContinue $ShortcutPath = ` Join-Path $Desktop $ShortcutName $Shell = New-Object -ComObject WScript.Shell $Shortcut = ` $Shell.CreateShortcut($ShortcutPath) $Shortcut.TargetPath = ` "$env:SystemRoot\System32\wscript.exe" $Shortcut.Arguments = ` "`"$Launcher`"" $Shortcut.WorkingDirectory = ` $InstallDir $Shortcut.Description = ` "Enable or disable onboard Ethernet" $Shortcut.IconLocation = ` "$env:SystemRoot\System32\ncpa.cpl,0" $Shortcut.Save() # ============================================================ # 5. Uninstaller # ============================================================ $UninstallCode = @' $ErrorActionPreference = "SilentlyContinue" $InstallDir = Join-Path $env:ProgramData "EthernetToggle" $TaskName = "Ethernet Toggle" $RegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\EthernetToggle" # ------------------------------------------------------------ # Self-elevate if required # ------------------------------------------------------------ $IsAdmin = ( New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent() ) ).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) if (-not $IsAdmin) { Start-Process ` powershell.exe ` -Verb RunAs ` -ArgumentList @( "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "`"$PSCommandPath`"" ) exit } # ------------------------------------------------------------ # Confirmation # ------------------------------------------------------------ Add-Type -AssemblyName PresentationFramework $result = [System.Windows.MessageBox]::Show( "Do you want to completely remove Ethernet Toggle?", "Uninstall Ethernet Toggle", "YesNo", "Question" ) if ($result -ne "Yes") { exit } # ------------------------------------------------------------ # Stop scheduled task # ------------------------------------------------------------ Stop-ScheduledTask ` -TaskName $TaskName ` -ErrorAction SilentlyContinue # ------------------------------------------------------------ # Remove scheduled task # ------------------------------------------------------------ Unregister-ScheduledTask ` -TaskName $TaskName ` -Confirm:$false ` -ErrorAction SilentlyContinue # ------------------------------------------------------------ # Remove desktop shortcut # ------------------------------------------------------------ $Desktop = [Environment]::GetFolderPath("Desktop") # Remove both possible localized shortcut names. # This also handles cases where Windows UI language was changed after installation. $cced = [char]231 $ShortcutNames = @( "Ethernet Toggle.lnk", "Ethernet A${cced}-Kapa.lnk" ) foreach ($ShortcutName in $ShortcutNames) { Remove-Item ` (Join-Path $Desktop $ShortcutName) ` -Force ` -ErrorAction SilentlyContinue } # Also remove both names from Public Desktop if either exists $PublicDesktop = ` [Environment]::GetFolderPath("CommonDesktopDirectory") if ($PublicDesktop) { foreach ($ShortcutName in $ShortcutNames) { Remove-Item ` (Join-Path $PublicDesktop $ShortcutName) ` -Force ` -ErrorAction SilentlyContinue } } # ------------------------------------------------------------ # Remove Programs & Features registration # ------------------------------------------------------------ Remove-Item ` $RegPath ` -Recurse ` -Force ` -ErrorAction SilentlyContinue # ------------------------------------------------------------ # Completion message # ------------------------------------------------------------ [System.Windows.MessageBox]::Show( "Ethernet Toggle has been uninstalled.", "Ethernet Toggle", "OK", "Information" ) | Out-Null # ------------------------------------------------------------ # Delete installation directory after this script exits # ------------------------------------------------------------ $CleanupCode = @" Start-Sleep -Seconds 2 Remove-Item -LiteralPath '$InstallDir' -Recurse -Force -ErrorAction SilentlyContinue "@ $Bytes = ` [System.Text.Encoding]::Unicode.GetBytes($CleanupCode) $Encoded = ` [Convert]::ToBase64String($Bytes) Start-Process ` powershell.exe ` -WindowStyle Hidden ` -ArgumentList @( "-NoProfile", "-NonInteractive", "-WindowStyle", "Hidden", "-EncodedCommand", $Encoded ) exit '@ Set-Content ` -LiteralPath $Uninstaller ` -Value $UninstallCode ` -Encoding UTF8 ` -Force # ============================================================ # 6. Register in Programs & Features # ============================================================ New-Item ` -Path $RegPath ` -Force | Out-Null $PowerShellExe = ` "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" $UninstallString = ` "`"$PowerShellExe`" -NoProfile -ExecutionPolicy Bypass -File `"$Uninstaller`"" New-ItemProperty ` -Path $RegPath ` -Name "DisplayName" ` -Value $AppName ` -PropertyType String ` -Force | Out-Null New-ItemProperty ` -Path $RegPath ` -Name "DisplayVersion" ` -Value $Version ` -PropertyType String ` -Force | Out-Null New-ItemProperty ` -Path $RegPath ` -Name "Publisher" ` -Value $Publisher ` -PropertyType String ` -Force | Out-Null New-ItemProperty ` -Path $RegPath ` -Name "InstallLocation" ` -Value $InstallDir ` -PropertyType String ` -Force | Out-Null New-ItemProperty ` -Path $RegPath ` -Name "DisplayIcon" ` -Value "$env:SystemRoot\System32\ncpa.cpl,0" ` -PropertyType String ` -Force | Out-Null New-ItemProperty ` -Path $RegPath ` -Name "UninstallString" ` -Value $UninstallString ` -PropertyType String ` -Force | Out-Null New-ItemProperty ` -Path $RegPath ` -Name "NoModify" ` -Value 1 ` -PropertyType DWord ` -Force | Out-Null New-ItemProperty ` -Path $RegPath ` -Name "NoRepair" ` -Value 1 ` -PropertyType DWord ` -Force | Out-Null New-ItemProperty ` -Path $RegPath ` -Name "EstimatedSize" ` -Value 32 ` -PropertyType DWord ` -Force | Out-Null # ============================================================ # 7. Installation completed # ============================================================ Add-Type -AssemblyName PresentationFramework [System.Windows.MessageBox]::Show( "Ethernet Toggle has been installed.`n`n" + "A shortcut has been created on the desktop.`n`n" + "You can uninstall it later from Programs & Features or Installed Apps.", "Ethernet Toggle", "OK", "Information" ) | Out-Null