During migration projects, I regularly change the DNS server settings of statically configured adapters on servers. (For clients, you can change the DHCP settings, and they will receive the new settings when the lease expires) For servers, it can be a lot of work doing it manually. In this blog post, I will show you how to automate that 🙂
How does the script work?
The script checks for network adapters that are up/connected and whether the primary or secondary configured DNS server is correct. If not, it will change the settings to match the primary and secondary DNS servers configured in the script.
Note: Run this on servers but not on Domain Controllers or servers with additional heartbeat network adapters. They are usually configured differently (127.0.0.1 as primary and one of the Domain Controllers in another location as secondary, for example)
How to run the script
For one device
You can copy the script to a folder on the server and run it as Administrator, configure the primary and secondary DNS server in the script, and the output will look like this: (Ony had one DNS server configured in this example. If you have two, they will both be displayed)

It shows the previous setting and what has been changed.
For multiple devices
And this is usually the case… You have a list of servers that need the DNS servers to be changed. To do that, you can use this after saving the script to your c:\scripts and use a servers.txt file containing all the server names that need the change:
foreach ($server in get-content c:\scripts\servers.txt) {Invoke-Command -Computername $server -FilePath C:\scripts\C:\Scripts\Set_DNS_Primary_Secondary_All_Adapters.ps1}
It will loop through all the servers and display the results for each server, just like the one above for one client.
The script
Below is the contents of the script:
#Requires -RunAsAdministrator #Retrieve all adapters which have a Up status $adapters = Get-NetAdapter | Where-Object Status -eq 'Up' #Set primary and secondary DNS Servers variables $primary = '10.0.0.4' $secondary = '10.0.0.5' #Loop through all adapters and configure $primary and $secondary for all adapters which have a DNS Server setting foreach ($adapter in $adapters) { if (Get-DnsClientServerAddress | Where-Object InterfaceIndex -eq $adapter.InterfaceIndex) { try { $dnsservers = (Get-NetIPConfiguration -InterfaceIndex $adapter.ifIndex -ErrorAction Stop).DNSServer.ServerAddresses Write-Host ("Retrieved DNS settings for adapter {0} on {1}" -f $adapter.Name, $env:COMPUTERNAME) } catch { Write-Warning ("Could not retrieve DNS settings for adapter {0} on {1}" -f $adapter.Name, $env:COMPUTERNAME) } if ($dnsservers -notcontains $primary -or $dnsservers -notcontains $secondary) { try { Set-DNSClientServerAddress -ServerAddresses ($primary, $secondary) -InterfaceIndex $adapter.ifIndex -ErrorAction Stop Write-Host ("Changing DNS settings for {0} to {1} and {2} (Previous setting was {3}) on {4}" -f $adapter.Name, $primary, $secondary, $($dnsservers -join ', '), $env:COMPUTERNAME) -ForegroundColor Green } catch { Write-Warning ("Error changing {0} on {1}" -f $adapter.Name, $env:COMPUTERNAME) } } else { Write-Host ("Adapter {0} already has {1} and {2} configured on {3}, skipping..." -f $adapter.Name, $primary, $secondary, $env:COMPUTERNAME) } } }
Download the script(s) from GitHub here