How to automate DNS server reconfiguration for TCP/IPv4 and TCP/IPv6 in Windows 7?

Solution 1:

From elevated command prompt (or from .bat file):

netsh interface ipv4 set dnsserver "Wireless Network Connection 2" source=static addr=8.8.8.8
netsh interface ipv4 add dnsserver "Wireless Network Connection 2" addr=8.8.4.4 index=2

or

netsh interface ipv4 set dnsserver "Wireless Network Connection 2" source=dhcp

You can just replace ipv4 with ipv6 (and proper address of course).

Solution 2:

In powershell:

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" 
$DNSServers = "8.8.8.8","8.8.4.4"  
$wmi.SetDNSServerSearchOrder($DNSServers)

To revert:

$wmi.SetDNSServerSearchOrder()

(no parameters=null, this will put it back in automatic mode)

References:

http://fatbeards.blogspot.com/2008/06/change-dnswins-ip-on-multiple-servers.html

http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/28/use-powershell-to-configure-static-ip-and-dns-settings.aspx