Append dns suffixes via windows command prompt

At my work, we have two connection specific DNS suffixes. lhs.local and cis.local. I'm trying to write a batch file that will take care of a lot of the common administrative tasks that need done when we deploy a computer, and appending these is one of those tasks.

Is there a command to do this programatically?


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