Change a NIC’s DNS Server settings with PowerShell and WMI: SetDNSServerSearchOrder()

We’re in the process of retiring a bunch of old domain controllers, and I needed an easy way to change the DNS server settings on all of my servers.

This is easily achieved in Windows 8 and Server 2012 and newer with the Get-NetAdapter and Set-DnsClientServerAddress cmdlets, but I needed a solution that would work on Server 2003 and 2008/R2.

Luckily this can be achieved with WMI, namely the Win32_NetworkAdapterConfiguration class and its accompanying method SetDNSServerSearchOrder.

Here’s a basic example of how you might do this on a single local machine with no error checking or feedback:

# The servers that we want to use
$newDNSServers = "192.168.1.1","192.168.1.2"

# Get all network adapters that already have DNS servers set
$adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DNSServerSearchOrder -ne $null}

# Set the DNS server search order for all of the previously-found adapters
$adapters | ForEach-Object {$_.SetDNSServerSearchOrder($newDNSServers)}

The useful thing with having a WMI-based approach is that you don’t depend on the target machine having to support PowerShell Remoting. You can use the –ComputerName parameter on Get-WmiObject to run this  remote machines.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s