4

The DNS server search order for a network interface can be read from Win32_NetworkAdapterConfiguration like this in Powershell, or programmatically using .Net ManagementObjects:

> $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq “TRUE”}
> $NICs[0]["DnsServerSearchOrder"]
192.168.1.1
192.168.1.2

The WMI property is set to the active DNS servers whether the interface is configured to get them from DHCP, or if they are set manually.

You can set them to fixed servers like this:

> $DNSServers = “198.102.234.125″,”198.102.234.126″
> $NIC.SetDNSServerSearchOrder($DNSServers)

To set an adapter to use DNS from a DHCP server, you call the set function with null as so:

> $NIC.SetDNSServerSearchOrder()

I was not able to find any distinctive traces of this setting in the registry.

Is there any way at all to tell that an interface is currently set to use DHCP to obtain its DNS servers?

3
  • Is Is there any way at all to tell an interface is currently set to use DHCP? the question or is it Is there any way at all to tell an interface is currently set to get its DNS servers from DHCP? -- big difference.
    – TheCleaner
    Mar 14, 2014 at 19:12
  • The question relates to only the DNS servers. The DHCPEnabled setting seems only to indicate if the address is obtained from DHCP.
    – Derrick
    Mar 14, 2014 at 19:26
  • 2
    Derrick,, OK...I'm editing your final sentence then so it doesn't throw anyone off.
    – TheCleaner
    Mar 14, 2014 at 19:29

3 Answers 3

3

Usually if a client is getting an IP from DHCP, it obtains DNS servers as well...usually.

But for your request for WMI, I don't think so.

For DNS you have these Properties:

------------------------------------ ---------- -------------------------
 Class Name                           Type       Property Name
------------------------------------ ---------- -------------------------
 Win32_ComputerSystem                 String     DNSHostName
 Win32_NetworkAdapterConfiguration    String     DNSDomain
 Win32_NetworkAdapterConfiguration    String     DNSDomainSuffixSearchOrder
 Win32_NetworkAdapterConfiguration    Boolean    DNSEnabledForWINSResolution
 Win32_NetworkAdapterConfiguration    String     DNSHostName
 Win32_NetworkAdapterConfiguration    String     DNSServerSearchOrder
 Win32_NetworkAdapterConfiguration    Boolean    DomainDNSRegistrationEnabled
 Win32_NetworkAdapterConfiguration    Boolean    FullDNSRegistrationEnabled
 Win32_NTDomain                       String     DnsForestName
 Win32_NTDomain                       Boolean    DSDnsControllerFlag
 Win32_NTDomain                       Boolean    DSDnsDomainFlag
 Win32_NTDomain                       Boolean    DSDnsForestFlag
------------------------------------ ---------- -------------------------

For DHCP you have:

------------------------------------ ---------- -------------------------
 Class Name                           Type       Property Name
------------------------------------ ---------- -------------------------
 Win32_NetworkAdapterConfiguration    Boolean    DHCPEnabled
 Win32_NetworkAdapterConfiguration    DateTime   DHCPLeaseExpires
 Win32_NetworkAdapterConfiguration    DateTime   DHCPLeaseObtained
 Win32_NetworkAdapterConfiguration    String     DHCPServer
------------------------------------ ---------- -------------------------

Out of those above...none specifically shows anything to tell you it is getting DNS servers from DHCP. DNSServerSearchOrder will list the servers in an array, but won't say "I got these from the DHCP Server.

EDIT: however, all that said about WMI, one way I do see is to use the old netsh command.

Specifically:

netsh interface ipv4 show dns

notice there will be a line called: "DNS servers configured through DHCP" if they are configured that way.

4
  • 1
    Nice find. The netsh command provides a great workaround to the deficiency in WMI. Will implement.
    – Derrick
    Mar 24, 2014 at 23:42
  • netsh command shows wrong result about L2TP connections. The DNS is set by DHCP, but netsh shows it is STATIC.
    – vadim_hr
    Feb 26, 2019 at 9:00
  • 1
    here is useful info about this issue stackoverflow.com/questions/13367306/…
    – vadim_hr
    Feb 26, 2019 at 12:08
  • 1
    If the DNS addresses are manually set on a Wi-Fi profile rather than on the Wi-Fi adapter netsh erroneously shows them as configured through DHCP. Feb 1 at 21:17
1

Run Get-WMIObject Win32_NetworkAdapterConfiguration on the system and you will see the first property output is:

DHCPEnabled:True

Unfortunately, that is the closest you are going to get to DHCP information and obviously it doesn't specify DNS info. From Microsofts documentation, the WMI object Win32_NEtworkAdapterConfiguration does not hold that setting.

http://msdn.microsoft.com/en-us/library/aa394217%28v=vs.85%29.aspx

3
  • The intent here is to [consensually] hijack the DNS servers and be able to restore the original configuration when done. It looks like using DHCPEnabled would at least let me make a better guess as to whether they were automatic or not. Thanks.
    – Derrick
    Mar 14, 2014 at 19:50
  • 1
    If you are trying to change DNS entries temporarily in a script you can pull the current DNS entries from DNSServerSearchOrder and store them until ready to revert. I'm not sure if you can use netsh interface ipv4 show dns output in your script but if so you could decide if you want to set static or dhcp dns.
    – Byron C.
    Mar 14, 2014 at 20:00
  • 1
    C: The trouble is if you write back what you read when the settings are "auto", it takes them off auto and fixes them, this is a big problem for users who roam between networks. I've had to make the assumption that if DHCP enabled is set to true, that the settings should be set back to auto. Not the most optimal situation, but the safer one. Thanks!
    – Derrick
    Mar 24, 2014 at 23:40
0

If DNS is static DNSserverSearchOrder is set... Then

Get-WmiObject Win32_NetworkAdapterConfiguration | ForEach-Object { if ($_.DNSServerSearchOrder){Write-Host "$($_.Description) ----> STATIC"}else{Write-Host "$($_.Description) ----> DHCP"}}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .