Append dns suffixes via windows command prompt

Via this post

In order to add a DNS suffix to a TCP/IP connection remotely, all you need is a list of IP addresses and the following command:

wmic /USER:administrator /PASSWORD:adminpassword /node:@c:\iplist.txt nicconfig call SetDNSSuffixSearchOrder (mydomain.com)

where C:\iplist.txt contains a list of IP addresses, line separated.

Another way is to add via the registry

reg add HKLM\System\currentcontrolset\services\tcpip\parameters /v “NV Domain” /d “mydomain.com” /f

There's a Microsoft KB entry for the same as well.


Based off of Sathya's answer and other resources, I wrote this:

@echo off
SETLOCAL EnableDelayedExpansion

:: Input here the additional suffix
set suffix=your.own.suffix

:: Get existing DNS suffixes
FOR /F "usebackq tokens=1,2* delims= " %%A in (`reg QUERY HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters /V SearchList ^| findstr REG_SZ`) do ( 
    set OLD_DNS=%%C
)

:: Check if it starts with our suffix
set OK=NO
FOR /F "tokens=1,2* delims=," %%A in ("%OLD_DNS%") do (
    if "%%A" == "%suffix%" set OK=YES
)

:: Add our suffix first if it's not there
if "%OK%" == "NO" (
    echo Conf KO: %OLD_DNS%
    reg add HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters /V SearchList /D "%suffix%,%OLD_DNS%" /F
) else (
    echo Conf OK: %OLD_DNS%
)

ipconfig /flushdns