WMI/netsh to add DNS servers on network adapters

Date posted: 2014-12-12
Last updated: 2026-10-04

How to add DNS servers to a Windows Server network adapter using WMI, netsh and PowerShell, for both IPv4 and IPv6 addresses.

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

The WMI method SetDNSServerSearchOrder() of Win32_NetworkAdapterConfiguration only sets IPv4 DNS servers, so use netsh interface ipv6 set dns and add dns ... index=N for the IPv6 resolvers. On current Windows versions, the Set-DnsClientServerAddress cmdlet does both in one go.

I originally wrote this post in 2014. The Get-WmiObject cmdlet used below only exists in Windows PowerShell 5.1, not in PowerShell 7, and the Win32_NetworkAdapter class is deprecated. The scripts still work in Windows PowerShell, but for new scripts use the DnsClient cmdlets shown at the end of this post.

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

The WMI class only adds IPv4 addresses on interfaces. Therefore we use netsh for IPv6. The class Win32_NetworkAdapter is used to look up 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

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

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

If your server has more than one network card - or adapter - and you want to change DNS servers only on the active, connected interface, 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* with your domain controllers' host names.

The modern way: Set-DnsClientServerAddress

On Windows Server 2012 and later, the DnsClient PowerShell module has the Set-DnsClientServerAddress cmdlet. It accepts IPv4 and IPv6 addresses in one list, so you don't need netsh anymore:

$adapter = ( Get-NetAdapter | where { $_.Status -like "Up" } ).Name
Set-DnsClientServerAddress -InterfaceAlias $adapter -ServerAddresses ("8.8.8.8", "8.8.4.4", "2001:4860:4860::8888", "2001:4860:4860::8844")

More network adapter tips: detect ethernet network speed in Windows. And if WMI queries start failing on busy servers, increase WMI memory to support a large volume of queries.

I write these posts in my spare time, based on real problems from my day job as a sysadmin. If this one saved you some debugging time, a small donation is much appreciated. Thanks! 🙏

Leave a Comment