Skip to content

Instantly share code, notes, and snippets.

@GeertHauwaerts
Created December 15, 2021 21:22
Show Gist options
  • Save GeertHauwaerts/e5a49ca91f1be9b938e56c6de4b4bd92 to your computer and use it in GitHub Desktop.
Save GeertHauwaerts/e5a49ca91f1be9b938e56c6de4b4bd92 to your computer and use it in GitHub Desktop.
Windows Batch: Add and Remove from Hosts
@REM ==
@REM Windows Batch: Add and Remove from Hosts
@REM ==
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET HOSTS=C:\Windows\System32\drivers\etc\hosts
SET DNS[0]=1.1.1.1 cat.example.com x-cat.example.com
SET DNS[1]=2.2.2.2 dog.example.com x-dog.example.com
SET DNS[2]=3.3.3.3 yak.example.com x-yak.example.com
for /l %%N in (0,1,2) do (
>NUL findstr /m /c:"!DNS[%%N]!" %HOSTS% && (
findstr /v /c:"!DNS[%%N]!" %HOSTS% > %TEMP%\hosts.tmp
move /y %TEMP%\hosts.tmp %HOSTS% > NUL
) || (
echo !DNS[%%N]! >> %HOSTS%
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment