@echo off

::preset IP
set Net_IP_Diy=172.16.38.20
set Net_MASK=255.255.255.0
set Net_gateway=172.16.38.1

::preset DNS
set Net_dnsPra=114.114.114.114
set Net_dnsOrder=114.114.115.115

title One-click switch IP address and DNS

:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else (
goto gotAdmin
)

:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"
exit /B

:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:-------------------------------------- 

:start

echo.
echo.
echo *******************  Change IP and DNS Tool  *******************
:Set_localTemp
setlocal enabledelayedexpansion

:Set_NetConnectionID
rem 获取设置网络连接名称
echo.
echo ********************   Select adapter name   *******************
echo.
set "str=DefaultIPGateway^,IPAddress^,DNSServerSearchOrder^,IPSubnet^,DHCPEnabled"
for /f "skip=1tokens=1*" %%i in ('wmic Nic where "Manufacturer<>'Microsoft' and NetConnectionStatus='2'" get Index^,NetConnectionID^') do (
    for /f "tokens=1* delims=:" %%j in ("%%j") do (
        set $%%i=%%j

    echo.
    echo Adapter Name: %%j
    set "NetConnectionName=%%j"

        for /f "tokens=1-3delims={}," %%a in ('wmic Nicconfig where "Index='%%i'" get %str% /value') do (
            for /f %%c in (%%c) do if /i "%%a" == "DNSServerSearchOrder=" set "DNS2=%%c"
            for /f %%c in ("%%a%%~b") do set "%%c"
        )
        if /i "!DHCPEnabled!" == "TRUE" (
          echo Adapter IP Mode : Auto
        ) else echo Adapter IP Mode : Manual 
    IF not defined Net_IP set Net_IP=!IPAddress! 
    echo Adapter IP Address : !IPAddress!     Preset IP Address : !Net_IP_Diy!
    IF not defined Net_MASK set Net_MASK=!IPSubnet!
    echo Adapter Subnet Mask !IPSubnet!       Preset Subnet Mask: !Net_MASK!
    IF not defined Net_gateway set Net_gateway=!DefaultIPGateway!
    echo Adapter Default Gateway !DefaultIPGateway!         Preset Default Gateway: !Net_gateway!
    IF not defined Net_dnsPra set Net_dnsPra=!DNSServerSearchOrder!
    echo Adapter Primary DNS !DNSServerSearchOrder!         Preset Primary DNS: !Net_dnsPra!
    IF defined dns2 set Net_dns2=!DNS2! &echo Adapter Backup DNS : !DNS2!        Preset Backup DNS: !Net_dns2!
    )
)
echo.

echo.
echo ============Plese select one option:============
echo ============1: Set IP to%Net_IP_Diy%============
echo ============2: Set IP to DHCP       ============
echo ============3: Set DNS to Preset DNS ===========
echo ============4: Set DNS to auto DNS =============
echo ============    Press Q to exit    =============
set /P var=":"
if %var%==1 goto ipstatic
if %var%==2 goto ipdhcp
if %var%==3 goto dnsstatic
if %var%==4 goto dnsdhcp
if %var%==q exit
echo Wrong input, please try again.
goto start

:ipstatic
echo %NetConnectionName%
echo Setting IP...
netsh interface ipv4 set address name=%NetConnectionName% source=static %Net_IP_Diy% %net_MASK% %Net_gateway%
ipconfig /flushdns
echo **IP set to %Net_IP_Diy%, subnet mask: %net_MASK%, default gateway: %Net_gateway%**
echo ------------------------------------------
goto start

:ipdhcp
netsh interface ip set address name=%NetConnectionName% source=dhcp
ipconfig /flushdns
echo **IP setting has been changed to DHCP**
echo ------------------------------------------
goto start

:dnsstatic
echo Setting DNS...
netsh interface ipv4 set dns name=%NetConnectionName% static addr=%Net_dnsPra% register=PRIMARY
netsh interface ipv4 add dns name=%NetConnectionName% addr=%Net_dnsOrder% index=2
ipconfig /flushdns
echo **Primary DNS has been set to %Net_dnsPra%, Backup DNS set to %Net_dnsOrder%**
echo ------------------------------------------
goto start

:dnsdhcp
echo Clearing DNS...
netsh interface ip delete dns %NetConnectionName% all
ipconfig /flushdns
echo **DNS has been reset**
echo ------------------------------------------
goto start