Skip to content

Instantly share code, notes, and snippets.

@MisaghM
Last active September 30, 2022 18:53
Show Gist options
  • Save MisaghM/5148c2cce7ab275676b46669a5eef7d2 to your computer and use it in GitHub Desktop.
Save MisaghM/5148c2cce7ab275676b46669a5eef7d2 to your computer and use it in GitHub Desktop.
A simple script to easily enable and disable DNS.
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
REM This script will (de)activate a statically configured DNS server for a network interface.
REM Call the script with enable/disable as the first command-line argument:
REM dns.bat enable (set static DNS server)
REM dns.bat disable (obtain DNS server automatically [DHCP])
REM Or just run the script to get a choice.
REM Additionally, you can use the second argument to set which interface to change.
REM Use `netsh interface show interface` to check your interface names.
IF NOT "%~2"=="" (
SET "interface=%~2"
) ELSE (
SET "interface=Wi-Fi" & REM Default interface name
)
SET "dns1=8.8.8.8" & REM Primary DNS
SET "dns2=8.8.4.4" & REM Secondary DNS
net session > nul 2>&1
IF ERRORLEVEL 1 (
ECHO Administrative permissions required.
GOTO end
)
netsh interface ip show config "%interface%" | findstr DNS
IF ERRORLEVEL 1 (
ECHO Invalid interface name: %interface%
PAUSE
EXIT /B 1
)
ECHO,
ECHO Primary DNS: %dns1%
ECHO Secondary DNS: %dns2%
ECHO,
IF /I "%~1"=="enable" CALL :enable & GOTO end
IF /I "%~1"=="disable" CALL :disable & GOTO end
CHOICE /C ED /M "[E]nable or [D]isable DNS"
IF ERRORLEVEL 2 CALL :disable
IF ERRORLEVEL 1 CALL :enable
GOTO end
:enable
IF NOT DEFINED dns2 (
netsh interface ip delete dns "%interface%" all > nul || EXIT /B
)
netsh interface ip set dns "%interface%" static %dns1% validate=no > nul || EXIT /B
netsh interface ip add dns "%interface%" %dns2% index=2 validate=no > nul || EXIT /B
ipconfig /flushdns > nul || EXIT /B
ECHO DNS Enabled.
EXIT /B 0
:disable
netsh interface ip set dns "%interface%" dhcp > nul || EXIT /B
ipconfig /flushdns > nul || EXIT /B
ECHO DNS Disabled.
EXIT /B 0
:end
PAUSE
EXIT /B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment