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

12,178

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

Solution 3

Some manufacturers (IBM/Lenovo, Sony to mention just a few) used to supply users with their own (branded) utilities to solve that. It mostly used on notebook PCs: when you notebook connected to 'known' network, the utility automatically set up whatever you need to live in that network (proxy, DNS, maybe secondary IPs on the same interfaces). If you're using that kind of system, try to use that option.

On the other hand, why don't you set that up on DCHP servers in that networks? Static (by MAC) settings are something not hard to implement, anyway.

Share:
12,178

Related videos on Youtube

syllogismos
Author by

syllogismos

Updated on September 18, 2022

Comments

  • syllogismos
    syllogismos almost 2 years

    I need to switch Internet Protocol Version 4 (TCP/IPv4) and Internet Protocol Version 6 (TCP/IPv6) settings for DNS server configuration from "Obtain DNS server address automatically" to hard coded values (for Google's DNS servers) and back. It's kind of a hassle to do manually (I need to go to Control Panel-Network and Internet-Network Connections, open Wireless Network Connection 2, then open Properties for the corresponding version, enter the DNS server values, and repeat for the other one). Is there a utility or script that would automate the process? Thanks.

  • syllogismos
    syllogismos about 12 years
    Thanks Lucas. I assume this is for IPv4. How do you get the WMI object for IPv6? Also, how do you switch it back to "Obtain DNS server address automatically"?
  • Lucas Kauffman
    Lucas Kauffman about 12 years
    I can't seem to find how to do it in powershell with IPv6. To obtain a DNS address automatically you would need to do a new DHCP request, there isn't a way (I'm aware off) to just got the DNS server from a DHCP server.
  • syllogismos
    syllogismos about 12 years
    That's okay. What about going back it back to "Obtain DNS server address automatically"?
  • Lucas Kauffman
    Lucas Kauffman about 12 years
    I'll add it to the answer
  • syllogismos
    syllogismos about 12 years
    Great, thanks. I'll mark is as the accepted answer, but if anyone has an idea hot to handle IPv6 or can offer other alternatives (e.g. do the same without PowerShell, please post it).
  • syllogismos
    syllogismos about 12 years
    Okay, just tried and it did not seem to work. After second statement, I just get the following output: __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 91
  • Lucas Kauffman
    Lucas Kauffman about 12 years
    Okay try this, I can't test it atm
  • syllogismos
    syllogismos about 12 years
    Great, thanks. Only for some reason the second command (the one that sets DNS server to 8.8.4.4) causes error: "'source' is not a valid argument for this command. The syntax supplied for this command is not valid. Check help for the correct syntax."
  • Dusan Bajic
    Dusan Bajic about 12 years
    ah, yes, source=static is not needed when adding the second nameserver. I have edited the answer
  • Alexander
    Alexander about 12 years
    No need to own the network to have some talk with 'network' guys so they create some settings for you (if it won't be against your corporate rules) :) Ok, here is the utilities for you to consider: Free IP Switcher or Net Profiles.
  • syllogismos
    syllogismos about 12 years
    Having some talk with 'network' guys? Ha-ha-ha... Very funny. Thanks for the links to the apps. They look like what I may have needed in the first place (would be better than a script option). Appreciate it.
  • syllogismos
    syllogismos about 12 years
    Okay, tried both and uninstalled. Net Profiles does not seem to do anything. Launched is normally and as an admin: nothing happens. IP Switcher looks a lot more messy than the script-based solution. Btw, when will people learn to build installers that automatically run as administrators. It's 2012 for crying out loud.
  • Alexander
    Alexander about 12 years
    Sorry to hear that, once one of it (Net Profiles) has helped me very nice, but that was on XP (and it was long ago and maybe different SP version). Look at NetSetMan, looks promising (and claims to be freeware for non-commercial purposes)...
  • Alexander
    Alexander about 12 years
    Just checked again - Net Profiles simple fails to start at all at my Windows 7 Prof x64 SP1. Sorry for recommending that