VBScript – Add a DNS suffix to the current list

If you need to append one or more DNS suffixes to the current list, you can use vbscript to add them to the registry key containing the list. Here is the code:

Set WSHShell = CreateObject("WScript.Shell")
Set WinShell = CreateObject("Shell.Application")
strDNSSuffixsToAdd = Array("domain1.intranet","domain2.intranet","subdom1.domain3.intranet") 'add here the list of DNS suffix you want to append
regDNSSuffixCurrent = LCase(WSHShell.RegRead("HKLM\System\CurrentControlSet\Services\TCPIP\Parameters\SearchList"))
regDNSSuffixUpdated = regDNSSuffixCurrent

For Each DNSSuffix In strDNSSuffixsToAdd
 If InStr(1,regDNSSuffixCurrent, DNSSuffix) > 0 Then
  WScript.Echo DNSSuffix & vbCRLF & " is already present in the DNS suffix search list"
 Else
  WScript.Echo DNSSuffix & vbCRLF & " added to the DNS suffix search list"
  regDNSSuffixUpdated = regDNSSuffixUpdated & "," & DNSSuffix
 End If
Next

WSHShell.RegWrite "HKLM\System\CurrentControlSet\Services\TCPIP\Parameters\SearchList", regDNSSuffixUpdated, "REG_SZ"
WinShell.ServiceStop "Dnscache",False
WinShell.ServiceStart "Dnscache",False
WScript.Echo
WScript.Echo "The 'DNS Client' service was restarted"

Code explaining:

regDNSSuffixCurrent = LCase(WSHShell.RegRead("HKLM\System\CurrentControlSet\Services\TCPIP\Parameters\SearchList"))

Retrieve the current list of DNS suffix from the registry.

If InStr(1,regDNSSuffixCurrent, DNSSuffix) > 0 Then

The script does not simply add the new suffixes to the end of the current list, it only adds missing ones.

WinShell.ServiceStop "Dnscache",False
WinShell.ServiceStart "Dnscache",False

The changes are not applied until you restart the computer or just restart the “DNS Client” service.

Leave a Reply

Your email address will not be published. Required fields are marked *