@echo off
setlocal EnableExtensions EnableDelayedExpansion

title Restore Windows Networking Defaults

rem -----------------------------------------------------------------
rem Restore Windows networking stack settings toward stock defaults.
rem Target: Windows 8 / 8.1 / 10 / 11 and newer, with runtime feature
rem detection so unsupported commands do not abort the reset flow.
rem -----------------------------------------------------------------

call :RequireAdmin || exit /b 1
call :InitTimestamp

set "BACKUP_DIR=%USERPROFILE%\Desktop\network_defaults_backup_%TIMESTAMP%"
set "LOGFILE=%BACKUP_DIR%\restore_actions.log"
mkdir "%BACKUP_DIR%" >nul 2>&1

>%LOGFILE% echo Restore started on %DATE% at %TIME%
>>%LOGFILE% echo Backup folder: %BACKUP_DIR%

cls
echo =====================================================================
echo   RESTORE WINDOWS NETWORKING DEFAULTS
echo =====================================================================
echo.
echo This script is intended to undo common network "optimizer" / tweak changes.
echo.
echo It will reset as much of the Windows networking stack as can be safely and
echo consistently reset from batch on Windows 8/8.1/10/11 and newer, including:
echo.
echo   - Winsock catalog and TCP/IP stack state
echo   - IPv4 and IPv6 user-configured stack settings
echo   - TCP global / heuristics / security / supplemental template settings
echo   - UDP global settings if the local Windows build supports them
echo   - Dynamic client port ranges for TCP and UDP, IPv4 and IPv6
echo   - Teredo / ISATAP / 6to4 / IP-HTTPS state where supported
echo   - WinHTTP proxy and portproxy configuration
echo   - Common registry tweaks used by "gaming" / "latency" scripts
echo.
echo Important:
echo   - A backup is created on your desktop before changes are applied.
echo   - Because Windows IP reset commands restore user-configured settings,
echo     static IP, DNS, gateway, WINS, and some route settings can be cleared.
echo   - The PC will restart automatically after you acknowledge the final prompt.
echo.
choice /c YN /n /m "Continue? [Y/N]: "
if errorlevel 2 exit /b 0

echo.
echo [1/7] Saving current configuration...
call :BackupState

echo [2/7] Removing common registry-based network tweaks...
call :RemoveRegistryTweaks

echo [3/7] Resetting Winsock and core IP stack...
call :ResetCoreStack

echo [4/7] Restoring TCP / UDP stack settings to default-oriented values...
call :ResetTcpUdp

echo [5/7] Restoring dynamic port ranges and tunnel state...
call :ResetPortsAndTunnels

echo [6/7] Clearing proxy / portproxy / DNS cache...
call :ResetProxyEtc

echo [7/7] Saving post-reset snapshot...
call :BackupAfterState

echo.
echo Finished.
echo Backup and logs were saved to:
echo   "%BACKUP_DIR%"
echo.
echo Windows will restart after you click OK on the next message.
echo.

powershell -NoProfile -ExecutionPolicy Bypass -Command "Add-Type -AssemblyName System.Windows.Forms; [void][System.Windows.Forms.MessageBox]::Show('Windows networking settings were restored as far back to defaults as this script can safely do on this version of Windows.' + [Environment]::NewLine + [Environment]::NewLine + 'The computer will restart now to finish applying the reset.' + [Environment]::NewLine + 'Click OK to restart.','Restart required',[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Warning)" >nul 2>&1
if errorlevel 1 (
    echo Press any key to restart now...
    pause >nul
)

shutdown /r /t 0 /d p:4:1 /c "Restarting to finish Windows networking reset." >nul 2>&1
exit /b 0

:RequireAdmin
powershell -NoProfile -ExecutionPolicy Bypass -Command "$p=New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()); if($p.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){exit 0}else{exit 1}" >nul 2>&1
if "%errorlevel%"=="0" exit /b 0
fltmc >nul 2>&1
if "%errorlevel%"=="0" exit /b 0

echo.
echo [ERROR] This script must be run as Administrator.
echo Right-click the file and choose "Run as administrator".
echo.
pause
exit /b 1

:InitTimestamp
for /f %%I in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "(Get-Date).ToString('yyyy-MM-dd_HHmmss')" 2^>nul') do set "TIMESTAMP=%%I"
if not defined TIMESTAMP set "TIMESTAMP=%DATE:/=-%_%TIME::=-%"
set "TIMESTAMP=%TIMESTAMP: =0%"
exit /b 0

:BackupState
ver > "%BACKUP_DIR%\os_version.txt" 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "$os=Get-CimInstance Win32_OperatingSystem -ErrorAction SilentlyContinue; if($os){$os | Select-Object Caption,Version,BuildNumber,OSArchitecture | Format-List *}" > "%BACKUP_DIR%\os_details.txt" 2>&1

netsh winsock dump > "%BACKUP_DIR%\winsock_dump_before.txt" 2>&1
netsh winsock show autotuning > "%BACKUP_DIR%\winsock_autotuning_before.txt" 2>&1
netsh winsock show catalog > "%BACKUP_DIR%\winsock_catalog_before.txt" 2>&1

netsh int ip show config > "%BACKUP_DIR%\ip_config_before.txt" 2>&1
netsh interface ipv4 dump > "%BACKUP_DIR%\ipv4_dump_before.txt" 2>&1
netsh interface ipv6 dump > "%BACKUP_DIR%\ipv6_dump_before.txt" 2>&1
netsh interface tcp dump > "%BACKUP_DIR%\tcp_dump_before.txt" 2>&1
netsh interface portproxy dump > "%BACKUP_DIR%\portproxy_dump_before.txt" 2>&1

netsh interface ipv4 show global > "%BACKUP_DIR%\ipv4_global_before.txt" 2>&1
netsh interface ipv6 show global > "%BACKUP_DIR%\ipv6_global_before.txt" 2>&1
netsh interface tcp show global > "%BACKUP_DIR%\tcp_global_before.txt" 2>&1
netsh interface tcp show heuristics > "%BACKUP_DIR%\tcp_heuristics_before.txt" 2>&1
netsh interface tcp show security > "%BACKUP_DIR%\tcp_security_before.txt" 2>&1
netsh interface tcp show supplemental template=automatic > "%BACKUP_DIR%\tcp_supplemental_automatic_before.txt" 2>&1
netsh interface tcp show supplemental template=internet > "%BACKUP_DIR%\tcp_supplemental_internet_before.txt" 2>&1
netsh interface tcp show supplemental template=compat > "%BACKUP_DIR%\tcp_supplemental_compat_before.txt" 2>&1
netsh interface tcp show supplemental template=datacenter > "%BACKUP_DIR%\tcp_supplemental_datacenter_before.txt" 2>&1
netsh interface tcp show supplemental template=custom > "%BACKUP_DIR%\tcp_supplemental_custom_before.txt" 2>&1
netsh interface tcp show supplementalports > "%BACKUP_DIR%\tcp_supplemental_ports_before.txt" 2>&1
netsh interface tcp show supplementalsubnets > "%BACKUP_DIR%\tcp_supplemental_subnets_before.txt" 2>&1

netsh interface udp show global > "%BACKUP_DIR%\udp_global_before.txt" 2>&1
netsh int ipv4 show dynamicport tcp > "%BACKUP_DIR%\ipv4_dynamic_tcp_before.txt" 2>&1
netsh int ipv4 show dynamicport udp > "%BACKUP_DIR%\ipv4_dynamic_udp_before.txt" 2>&1
netsh int ipv6 show dynamicport tcp > "%BACKUP_DIR%\ipv6_dynamic_tcp_before.txt" 2>&1
netsh int ipv6 show dynamicport udp > "%BACKUP_DIR%\ipv6_dynamic_udp_before.txt" 2>&1
netsh interface ipv4 show excludedportrange tcp > "%BACKUP_DIR%\ipv4_excluded_tcp_before.txt" 2>&1
netsh interface ipv4 show excludedportrange udp > "%BACKUP_DIR%\ipv4_excluded_udp_before.txt" 2>&1
netsh interface ipv6 show excludedportrange tcp > "%BACKUP_DIR%\ipv6_excluded_tcp_before.txt" 2>&1
netsh interface ipv6 show excludedportrange udp > "%BACKUP_DIR%\ipv6_excluded_udp_before.txt" 2>&1

netsh interface teredo show state > "%BACKUP_DIR%\teredo_before.txt" 2>&1
netsh interface ipv6 isatap show state > "%BACKUP_DIR%\isatap_state_before.txt" 2>&1
netsh interface ipv6 isatap show router > "%BACKUP_DIR%\isatap_router_before.txt" 2>&1
netsh interface 6to4 show state > "%BACKUP_DIR%\6to4_state_before.txt" 2>&1
netsh interface httpstunnel show interfaces > "%BACKUP_DIR%\httpstunnel_before.txt" 2>&1

netsh interface portproxy show all > "%BACKUP_DIR%\portproxy_before.txt" 2>&1
netsh winhttp show proxy > "%BACKUP_DIR%\winhttp_proxy_before.txt" 2>&1
ipconfig /all > "%BACKUP_DIR%\ipconfig_all_before.txt" 2>&1
route print > "%BACKUP_DIR%\route_print_before.txt" 2>&1
arp -a > "%BACKUP_DIR%\arp_before.txt" 2>&1

powershell -NoProfile -ExecutionPolicy Bypass -Command "if(Get-Command Get-NetTCPSetting -ErrorAction SilentlyContinue){Get-NetTCPSetting | Format-List *}" > "%BACKUP_DIR%\Get-NetTCPSetting_before.txt" 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "if(Get-Command Get-NetTransportFilter -ErrorAction SilentlyContinue){Get-NetTransportFilter | Format-List *}" > "%BACKUP_DIR%\Get-NetTransportFilter_before.txt" 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "if(Get-Command Get-NetOffloadGlobalSetting -ErrorAction SilentlyContinue){Get-NetOffloadGlobalSetting | Format-List *}" > "%BACKUP_DIR%\Get-NetOffloadGlobalSetting_before.txt" 2>&1

reg export "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "%BACKUP_DIR%\tcpip_parameters_before.reg" /y >nul 2>&1
reg export "HKLM\SYSTEM\CurrentControlSet\Services\Dhcp\Parameters" "%BACKUP_DIR%\dhcp_parameters_before.reg" /y >nul 2>&1
reg export "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters" "%BACKUP_DIR%\tcpip6_parameters_before.reg" /y >nul 2>&1
reg export "HKLM\SYSTEM\CurrentControlSet\Services\AFD\Parameters" "%BACKUP_DIR%\afd_parameters_before.reg" /y >nul 2>&1
reg export "HKLM\SYSTEM\CurrentControlSet\Services\WinSock2\Parameters" "%BACKUP_DIR%\winsock2_parameters_before.reg" /y >nul 2>&1
reg export "HKLM\SOFTWARE\Policies\Microsoft\Windows\Psched" "%BACKUP_DIR%\psched_before.reg" /y >nul 2>&1
reg export "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile" "%BACKUP_DIR%\systemprofile_before.reg" /y >nul 2>&1
exit /b 0

:RemoveRegistryTweaks
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "DefaultTTL"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "EnablePMTUDiscovery"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "EnablePMTUBHDetect"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "GlobalMaxTcpWindowSize"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "TcpWindowSize"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "Tcp1323Opts"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "SackOpts"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "TcpMaxDupAcks"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "TcpMaxDataRetransmissions"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "SynAttackProtect"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "EnableDCA"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "EnableTCPA"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "TcpTimedWaitDelay"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "MaxUserPort"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "DisableTaskOffload"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "KeepAliveTime"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" "KeepAliveInterval"

call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters" "DisabledComponents"

call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\AFD\Parameters" "DefaultSendWindow"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\AFD\Parameters" "DefaultReceiveWindow"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\AFD\Parameters" "DynamicSendBufferDisable"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\AFD\Parameters" "FastSendDatagramThreshold"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\AFD\Parameters" "IgnorePushBitOnReceives"
call :DeleteRegValue "HKLM\SYSTEM\CurrentControlSet\Services\AFD\Parameters" "NonBlockingSendSpecialBuffering"

call :DeleteRegValue "HKLM\SOFTWARE\Policies\Microsoft\Windows\Psched" "NonBestEffortLimit"
call :DeleteRegValue "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile" "NetworkThrottlingIndex"
call :DeleteRegValue "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile" "SystemResponsiveness"

for /f "delims=" %%K in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces" 2^>nul ^| findstr /R /C:"\\Interfaces\\{.*}"') do (
    call :DeleteRegValue "%%K" "TcpAckFrequency"
    call :DeleteRegValue "%%K" "TCPNoDelay"
    call :DeleteRegValue "%%K" "TcpDelAckTicks"
    call :DeleteRegValue "%%K" "TcpInitialRTT"
)
exit /b 0

:ResetCoreStack
call :RunCommand netsh winsock set autotuning on
call :RunCommand netsh winsock reset
call :RunCommand netsh int ip reset "%BACKUP_DIR%\netsh_int_ip_reset.log"
call :RunCommand netsh interface ipv4 reset
call :RunCommand netsh interface ipv6 reset
exit /b 0

:ResetTcpUdp
rem Apply TCP global settings one-by-one so one unsupported switch does not
rem invalidate the entire command line on older Windows builds.
call :SetTcpGlobalIfSupported "rss=" "default"
call :SetTcpGlobalIfSupported "autotuninglevel=" "normal"
call :SetTcpGlobalIfSupported "ecncapability=" "disabled"
call :SetTcpGlobalIfSupported "timestamps=" "disabled"
call :SetTcpGlobalIfSupported "initialrto=" "3000"
call :SetTcpGlobalIfSupported "rsc=" "default"
call :SetTcpGlobalIfSupported "nonsackrttresiliency=" "default"
call :SetTcpGlobalIfSupported "maxsynretransmissions=" "2"
call :SetTcpGlobalIfSupported "fastopen=" "default"
call :SetTcpGlobalIfSupported "fastopenfallback=" "default"
call :SetTcpGlobalIfSupported "hystart=" "default"
call :SetTcpGlobalIfSupported "prr=" "default"
call :SetTcpGlobalIfSupported "pacingprofile=" "default"

call :SetTcpHeuristicsIfSupported "wsh=" "default"
call :SetTcpHeuristicsIfSupported "forcews=" "default"

call :SetTcpSecurityIfSupported "mpp=" "default"
call :SetTcpSecurityIfSupported "profiles=" "default"

call :RestoreCongestionProvider

powershell -NoProfile -ExecutionPolicy Bypass -Command "if(Get-Command Get-NetTransportFilter -ErrorAction SilentlyContinue){Get-NetTransportFilter -ErrorAction SilentlyContinue | Remove-NetTransportFilter -Confirm:`$false -ErrorAction SilentlyContinue}" >> "%LOGFILE%" 2>&1

call :NetshHelpHas "interface udp set global ?" "uro=" HAS_URO
call :NetshHelpHas "interface udp set global ?" "uso=" HAS_USO
if "!HAS_URO!"=="1" call :RunCommand netsh interface udp set global uro=default
if "!HAS_USO!"=="1" call :RunCommand netsh interface udp set global uso=default
call :RunCommand netsh interface udp reset
exit /b 0

:RestoreCongestionProvider
set "CONGESTION_DONE=0"

rem Newer Windows builds expose congestion provider under supplemental templates.
call :NetshHelpHas "interface tcp set supplemental ?" "congestionprovider=" HAS_SUPP_CONGESTION
if "!HAS_SUPP_CONGESTION!"=="1" (
    call :NetshHelpHas "interface tcp set supplemental ?" "cubic" HAS_SUPP_CUBIC
    if "!HAS_SUPP_CUBIC!"=="1" (
        call :RunCommand netsh interface tcp set supplemental template=internet congestionprovider=cubic
        call :RunCommand netsh interface tcp set supplemental template=automatic congestionprovider=cubic
        set "CONGESTION_DONE=1"
    ) else (
        call :NetshHelpHas "interface tcp set supplemental ?" "ctcp" HAS_SUPP_CTCP
        if "!HAS_SUPP_CTCP!"=="1" (
            call :RunCommand netsh interface tcp set supplemental template=internet congestionprovider=ctcp
            call :RunCommand netsh interface tcp set supplemental template=automatic congestionprovider=ctcp
            set "CONGESTION_DONE=1"
        )
    )
)
if "!CONGESTION_DONE!"=="1" exit /b 0

rem Older Windows builds use the legacy global congestionprovider switch.
call :NetshHelpHas "interface tcp set global ?" "congestionprovider=" HAS_GLOBAL_CONGESTION
if "!HAS_GLOBAL_CONGESTION!"=="1" (
    call :NetshHelpHas "interface tcp set global ?" "cubic" HAS_GLOBAL_CUBIC
    if "!HAS_GLOBAL_CUBIC!"=="1" (
        call :RunCommand netsh interface tcp set global congestionprovider=cubic
        set "CONGESTION_DONE=1"
    ) else (
        call :NetshHelpHas "interface tcp set global ?" "ctcp" HAS_GLOBAL_CTCP
        if "!HAS_GLOBAL_CTCP!"=="1" (
            call :RunCommand netsh interface tcp set global congestionprovider=ctcp
            set "CONGESTION_DONE=1"
        )
    )
)

if "!CONGESTION_DONE!"=="0" >> "%LOGFILE%" echo [INFO] No supported congestion-provider command was detected on this Windows build.
exit /b 0

:SetTcpGlobalIfSupported
call :NetshHelpHas "interface tcp set global ?" "%~1" _HAS
if "!_HAS!"=="1" call :RunCommand netsh interface tcp set global %~1%~2
exit /b 0

:SetTcpHeuristicsIfSupported
call :NetshHelpHas "interface tcp set heuristics ?" "%~1" _HAS
if "!_HAS!"=="1" call :RunCommand netsh interface tcp set heuristics %~1%~2
exit /b 0

:SetTcpSecurityIfSupported
call :NetshHelpHas "interface tcp set security ?" "%~1" _HAS
if "!_HAS!"=="1" call :RunCommand netsh interface tcp set security %~1%~2
exit /b 0

:ResetPortsAndTunnels
call :RunCommand netsh int ipv4 set dynamicport tcp start=49152 num=16384
call :RunCommand netsh int ipv4 set dynamicport udp start=49152 num=16384
call :RunCommand netsh int ipv6 set dynamicport tcp start=49152 num=16384
call :RunCommand netsh int ipv6 set dynamicport udp start=49152 num=16384

call :RunCommand netsh interface teredo set state type=default servername=default refreshinterval=default clientport=default
call :RunCommand netsh interface ipv6 set teredo type=default servername=default clientport=default

call :RunCommand netsh interface ipv6 isatap set state state=default
call :RunCommand netsh interface ipv6 isatap set router name=default state=default
call :RunCommand netsh interface isatap set state state=default
call :RunCommand netsh interface isatap set router name=default state=default

call :RunCommand netsh interface 6to4 set state state=default
call :RunCommand netsh interface 6to4 set relay name=default state=default
call :RunCommand netsh interface 6to4 set routing routing=default sitelocals=default
call :RunCommand netsh interface ipv6 6to4 set state state=default
call :RunCommand netsh interface ipv6 6to4 set relay name=default state=default
call :RunCommand netsh interface ipv6 6to4 set routing routing=default sitelocals=default

call :RunCommand netsh interface httpstunnel reset
exit /b 0

:ResetProxyEtc
call :RunCommand netsh interface portproxy reset
call :RunCommand netsh winhttp reset proxy
call :RunCommand netsh winhttp reset autoproxy
call :RunCommand ipconfig /flushdns
exit /b 0

:BackupAfterState
netsh winsock show autotuning > "%BACKUP_DIR%\winsock_autotuning_after.txt" 2>&1
netsh winsock show catalog > "%BACKUP_DIR%\winsock_catalog_after.txt" 2>&1
netsh interface ipv4 show global > "%BACKUP_DIR%\ipv4_global_after.txt" 2>&1
netsh interface ipv6 show global > "%BACKUP_DIR%\ipv6_global_after.txt" 2>&1
netsh interface tcp show global > "%BACKUP_DIR%\tcp_global_after.txt" 2>&1
netsh interface tcp show heuristics > "%BACKUP_DIR%\tcp_heuristics_after.txt" 2>&1
netsh interface tcp show security > "%BACKUP_DIR%\tcp_security_after.txt" 2>&1
netsh interface tcp show supplemental template=automatic > "%BACKUP_DIR%\tcp_supplemental_automatic_after.txt" 2>&1
netsh interface tcp show supplemental template=internet > "%BACKUP_DIR%\tcp_supplemental_internet_after.txt" 2>&1
netsh interface udp show global > "%BACKUP_DIR%\udp_global_after.txt" 2>&1
netsh int ipv4 show dynamicport tcp > "%BACKUP_DIR%\ipv4_dynamic_tcp_after.txt" 2>&1
netsh int ipv4 show dynamicport udp > "%BACKUP_DIR%\ipv4_dynamic_udp_after.txt" 2>&1
netsh int ipv6 show dynamicport tcp > "%BACKUP_DIR%\ipv6_dynamic_tcp_after.txt" 2>&1
netsh int ipv6 show dynamicport udp > "%BACKUP_DIR%\ipv6_dynamic_udp_after.txt" 2>&1
netsh interface teredo show state > "%BACKUP_DIR%\teredo_after.txt" 2>&1
netsh interface portproxy show all > "%BACKUP_DIR%\portproxy_after.txt" 2>&1
netsh winhttp show proxy > "%BACKUP_DIR%\winhttp_proxy_after.txt" 2>&1
ipconfig /all > "%BACKUP_DIR%\ipconfig_all_after.txt" 2>&1
route print > "%BACKUP_DIR%\route_print_after.txt" 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "if(Get-Command Get-NetTCPSetting -ErrorAction SilentlyContinue){Get-NetTCPSetting | Format-List *}" > "%BACKUP_DIR%\Get-NetTCPSetting_after.txt" 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "if(Get-Command Get-NetTransportFilter -ErrorAction SilentlyContinue){Get-NetTransportFilter | Format-List *}" > "%BACKUP_DIR%\Get-NetTransportFilter_after.txt" 2>&1
exit /b 0

:RunCommand
>>"%LOGFILE%" echo.
>>"%LOGFILE%" echo [RUN] %*
call %* >> "%LOGFILE%" 2>&1
set "_RC=!ERRORLEVEL!"
if not "!_RC!"=="0" >> "%LOGFILE%" echo [WARN] Exit code !_RC! for: %*
exit /b 0

:DeleteRegValue
reg query "%~1" /v "%~2" >nul 2>&1
if not errorlevel 1 (
    >>"%LOGFILE%" echo [REG] Deleting %~1\%~2
    reg delete "%~1" /v "%~2" /f >> "%LOGFILE%" 2>&1
)
exit /b 0

:NetshHelpHas
set "%~3=0"
set "_TMP=%TEMP%\netsh_help_%RANDOM%_%RANDOM%.txt"
netsh %~1 > "!_TMP!" 2>&1
find /I "%~2" "!_TMP!" >nul 2>&1 && set "%~3=1"
del /q "!_TMP!" >nul 2>&1
exit /b 0
