WMI/netsh to add DNS servers on network adapters

How to add DNS servers – or resolvers – to a Windows Server network adapter, or interface with WMI and the netsh command. This one is quite old but may come in handy sometimes. In this example we use Google’s Public DNS server addresses and localhost to add as DNS Servers on our server.

WMI and netsh to add DNS servers on network adapters for IPv4 and IPv6 addresses #

The WMI class only can add IPv4 addresses on interfaces. Therefor we use netsh for IPv6. The class Win32_NetworkAdapter is used to lookup the interface name and that interface is configured with the Win32_NetworkAdapterConfiguration WMI class.

AddDnsServers.ps1:

[array]$nics = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=true"
$dnsservers = "127.0.0.1", "8.8.8.8", "8.8.4.4"
$nics[0].SetDNSServerSearchOrder($dnsservers)
$index = $nics[0].index
$adapter = (Get-WmiObject -Class Win32_NetworkAdapter -Filter "DeviceID=$index").NetConnectionID

netsh interface ipv6 set dns "$adapter" static "2001:4860:4860::8888" primary
netsh interface ipv6 add dns "$adapter" "2001:4860:4860::8844" index=2

If you want to add more than two DNS name servers, increase the index number:

netsh interface ipv6 set dns "$adapter" static "::1" primary
netsh interface ipv6 add dns "$adapter" "2001:4860:4860::8888" index=2
netsh interface ipv6 add dns "$adapter" "2001:4860:4860::8844" index=3

Note: The Win32_NetworkAdapter class is deprecated. Use the MSFT_NetAdapter class instead.

I thought you might find this interesting:   KB3157663: Cumulative Update for Windows Server 2016 Technical Preview 5

In a multi NIC environment, set DNS servers for one active network card only #

If your server has more than one network cards – or adapters – and suppose you want to change DNS servers only on active connected interfaces, then you can use something like:

$adapter = ( Get-NetAdapter | where { $_.Status -like "Up" } ).Name
netsh interface ipv4 set dns "$adapter" static "8.8.8.8" primary
netsh interface ipv4 add dns "$adapter" "8.8.4.4" index=2
netsh interface ipv4 add dns "$adapter" "208.67.222.222" index=3
netsh interface ipv6 set dns "$adapter" static "2001:4860:4860::8888" primary
netsh interface ipv6 add dns "$adapter" "2001:4860:4860::8844" index=2
netsh interface ipv6 add dns "$adapter" "2620:0:ccc::2" index=3

if ( $env:computername.tolower() -like "ad*" ) {
	write-host "[*] this is a Domain Controller! Add localhost as well, as last"
	netsh interface ipv4 add dns "$adapter" "127.0.0.1" index=4
	netsh interface ipv6 add dns "$adapter" "::1" index=4
}

Write-Host "DNS resolvers changed"

Substitute ad* for your domain controllers host names


Please Support Saotn.org

Each post on Sysadmins of the North takes a significant amount of time to research, write, and edit. Therefore, your donation helps a lot! For example, a donation of $3 U.S. buys me a cup of coffee, and as you know: things jsut work better with coffee. A $10 U.S. donation buys me one month of web hosting (yes, hosting costs money). But seriously, thank you for any amount. Much appreciated!

Please donate to support this site if you found a post interesting or if it helped you solve a problem. Thanks! (Tip: no Paypal account required)

If you appreciated this post, then please donate using this Paypal button


Jan Reilink

My name is Jan. I am not a hacker, coder, developer, programmer or guru. I am merely a system administrator, doing my daily thing at Vevida in the Netherlands. With over 15 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization.

Leave a Reply

1 Comment on "WMI/netsh to add DNS servers on network adapters"

Hi! Join the discussion, leave a reply!

Sort by:   newest | oldest | most voted
Fernanda
Guest

Hi, do you have this script working in vbs file?