1

#PSTip How to disable DNS registration with PowerShell

Note: This tip requires PowerShell 2.0 or above.

Today, a colleague in the network team asked if we could disable the default DNS registration flag (shown below) because our computers are not allowed to update DNS servers directly.

image001

As only Windows 7 computers are targeted, WMI is the correct way to go. We first need to find the WMI properties related to the DNS configuration of the network card.

(Get-WmiObject Win32_NetworkAdapter -Filter "NetEnabled=True").GetRelated('Win32_NetworkAdapterConfiguration') |
Format-List -Property *

image002

The second step consists of using the Get-Member cmdlet to discover the method designed to configure the DNS registration flag.

(Get-WmiObject Win32_NetworkAdapter -Filter "NetEnabled=True").GetRelated('Win32_NetworkAdapterConfiguration') |
Get-Member -MemberType Method

image004

“Invoking” the method without any parenthesis will then show the parameters to be passed to the method:

(Get-WmiObject Win32_NetworkAdapter -Filter "NetEnabled=True").
GetRelated('Win32_NetworkAdapterConfiguration').
SetDynamicDNSRegistration

image006

To test the method and its effect, the following can be done:

(Get-WmiObject Win32_NetworkAdapter -Filter "NetEnabled=True").GetRelated('Win32_NetworkAdapterConfiguration').SetDynamicDNSRegistration($false,$false)

Desktops usually have only one network card and the above code would work fine on PowerShell 2.0 in this case. But the above code will also produce an error if there’s more than one network card. The above code isn’t actually suitable for laptops. To get the code work on laptops with PowerShell 2.0, we need to pipe it to the ForEach-Object cmdlet:

(Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetEnabled=True").GetRelated('Win32_NetworkAdapterConfiguration') | ForEach-Object {
    $_.SetDynamicDNSRegistration($false,$false)
}

On PowerShell 3.0 the following would disable the DNS registration flag on all network cards because of the implicit foreach feature

(Get-WmiObject Win32_NetworkAdapter -Filter "NetEnabled=True").GetRelated('Win32_NetworkAdapterConfiguration').SetDynamicDNSRegistration($false,$false)

And, if we had Windows 8/2012 or above, the correct way to go would have been to use the built-in cmdlets like this:

Get-NetIPConfiguration |
Get-NetConnectionProfile |
Where IPv4Connectivity -ne "NoTraffic" |
Set-DnsClient -RegisterThisConnectionsAddress:$false -Verbose

The above where filter can also be avoided like this:

Get-NetConnectionProfile -IPv4Connectivity Internet,Disconnected,LocalNetwork,Subnet -ErrorAction SilentlyContinue |
Set-DnsClient -RegisterThisConnectionsAddress:$false -Verbose

Last but not least, DNS records might not be automatically removed from DNS servers. You may need to follow additional steps from this knowledge based article whether your network card was configured to use a static or a dynamic IP address: http://support.microsoft.com/kb/2933537/en-us.

Filed in: Columns, Tips and Tricks Tags: , , ,

One Response to "#PSTip How to disable DNS registration with PowerShell"

  1. Roger says:

    Hi Emin,

    What about if I want this to be applied to a particular NIC and not to all of them ?

    Thanks in advance,

    RJC

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: