Using PowerShell to set DNS search order on Windows

Adam Bertram

Adam Bertram

Read more posts by this author.

Do you need to set the DNS search order on a whole bunch of workstations or servers? Do you have PowerShell? Then you’re in luck because I’m about to show you how to use PowerShell to set DNS search order on lots of machines at once!

DNS Server Search Order (GUI-Style)
DNS Server Search Order (GUI-Style)

Finding Existing Search Order

First, find the current DNS server search order on your local Windows machine. This will allow you to find the code necessary to expand this functionality to other machines in a little bit.

Start with the Get-DNSClientServerAddress command. This command enumerates each network interface along with the DNS server addresses bound to it. You can see below that we have an Ethernet 2 interface that has the DNS server addresses assigned you can see in the GUI screenshot above.

PS51> Get-DNSClientServerAddress

InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
Ethernet 2                          16 IPv4    {1.1.1.1, 2.2.2.2}
--snip--

Taking a look at the old school ipconfig /all output, you can see that this is, indeed, the correct one.

ipconfig /all output
ipconfig /all output

Changing DNS Server Search Order

Now, change the order of the DNS servers from 1.1.1.1, 2.2.2.2 to 2.2.2.2, 1.1.1.1. To do that, use the Set-DNSClientServerAddress cmdlet. Point that command to the interface you’d like to change (16 in this case) and provide the AddressFamily parameter to IPV4.

One way the interface number can be found is by running Get-DNSClientServerAddress with no parameters as we did above.

PS51> $serverAddresses = Get-DNSClientServerAddress -InterfaceIndex 16 -AddressFamily IPv4

InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
Ethernet 2                          16 IPv4    {1.1.1.1, 2.2.2.2}

You should now have the interface assigned to the $serverAddresses variable which contains a ServerAddresses property which is an array of addresses as shown below. Assign the ServerAddresses property to another variable because you’ll be modifying this value.

PS51> $serverAddresses = Get-DNSClientServerAddress -InterfaceIndex 16 -AddressFamily IPv4
PS51> $dnsIps = $serverAddresses.ServerAddresses
PS51> $dnsIps
1.1.1.1
2.2.2.2

Now you need to switch these around. The $dnsIps array should represent the server addresses flip-flopped.

PS51> $dnsIps[0] = $serverAddresses.ServerAddresses[1]
PS51> $dnsIps[1] = $serverAddresses.ServerAddresses[0]
PS51> $dnsIps
2.2.2.2
1.1.1.1

Now that the $dnsIps array contains the preferred order, you can now commit the change to the interface using Set-DnsClientServerAddress.

PS51> $serverAddresses | Set-DnsClientServerAddress -ServerAddresses $dnsIps
PS51> Get-DNSClientServerAddress -InterfaceIndex 16 -AddressFamily IPv4

InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
Ethernet 2                          16 IPv4    {2.2.2.2, 1.1.1.1}

Expanding to Multiple Computers

Once you have learned how to perform this task on one computer, expanding that to lots of computers is easy with PowerShell using PowerShell Remoting. You will simply need to wrap the code just covered in a scriptblock and specify all of the computers to execute on.

$scriptBlock = {
    $serverAddresses = Get-DNSClientServerAddress -InterfaceIndex 16 -AddressFamily IPv4

    $dnsIps = $serverAddresses.ServerAddresses
    $dnsIps[0] = $serverAddresses.ServerAddresses[1]
    $dnsIps[1] = $serverAddresses.ServerAddresses[0]

    $serverAddresses | Set-DnsClientServerAddress -ServerAddresses $dnsIps
}

## Find a list of computers from somewhere
$computers = @('SRV1','SRV2','SRV3')

## Run the scriptblock on all of them
Invoke-Command -ComputerName $computers -ScriptBlock $scriptBlock

Summary

You now have the basic knowledge to set DNS server search order with PowerShell. We discovered existing DNS servers and then switched them. However, know that you don’t have to use this approach. As long as you have the DNS server IPs saved into an array and passed to Set-DnsClientServerAddress any IP addresses will work.

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!