In this post, I will take you through a script that will help you set DNS server IP addresses in the network adapter properties of local and remote computers.

The script uses a method called SetDNSServerSearchOrder of the Win32_NetworkAdapterConfiguration WMI class to set DNS servers. See MSDN for more details about this WMI class. This method takes an array containing IP addresses of DNS servers.

The script takes two arguments: ComputerName and DNSServers. The DNSServers argument is a mandatory input without which the script will not continue. You can provide up to two IP addresses but not more than that. This is because you cannot set more than two DNS server IP addresses for a given network adapter.

The script starts with the following code, which does a couple of important checks before it continues to the main code. The IF condition verifies to see if more than two DNS server IP addresses are specified, and the FOR loop checks if given IP addresses are in a valid IP address format. It is necessary to check the IP address format before setting IP addresses as DNS servers for the network adapter. Otherwise, the script will throw errors when it executes the SetDNSServerSearchOrder method.

if($DNSServers.Count -gt 2) {
Write-Error "You cannot set more than two DNS servers"
foreach($DNSIP in $DNSServers) {
if(![bool]($DNSIP -as [IPAddress])) {
Write-Error "Invalid IP Address. Script exiting"

The main script is a simple wrapper around querying the Win32_NetworkAdapterConfiguration WMI class for IP-enabled network connections and executing the SetDNSServerSearchOrder method against them. The following code illustrates these actions.

$Networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration 
         -Filter "IPenabled=TRUE"
if($Networks.Count -gt 1) {
Write-Host "The $Computer has more than two network
connections." -ForeGroundColor red
foreach($Network in $Networks) {
try {
$Network.SetDNSServerSearchOrder($DNSServers) | Out-Null
} catch {
Write-Host "FAILED to set DNS Servers" -ForeGroundColor red

In the above code, notice that I am actually checking how many network adapters are returned from the WMI query and then proceeding to the next computer if the number of network connections is more than 2. I did this for the sake of safety because, in most cases, we don’t want to set DNS servers for all network connections in a computer. If you have a requirement to do so, you can remove the part that checks the number of connections.

I also use a try-catch statement to catch any unknown exceptions and display the error messages when the DNS servers are set. This will help in handling the exceptions and proceeding with the next computers, if any.

One thing you should be aware of here is that, if you are running this script against a computer that has a DHCP given IP address, this script will still change the DNS server’s IP addresses by enabling “Use the following DNS Server addresses” in the network connection properties. If you want to run the script against network connections that only have static IP addresses configured, you can update the WMI query to match the following example. Using this query, we are generating a list of network adapters that have static IP addresses.

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName 
$Computer -Filter "IPEnabled=TRUE and DHCPEnabled=FALSE"


.\Set-DNSServers.ps1 -Computer Mytestpc1 -DNSServers,

Incorrectly setting the DNS servers can cause serious problems. Therefore, test the script in a test environment to ensure it functions as expected before you execute it in production.

Subscribe to 4sysops newsletter!

In my next post I will show you how to get DNS server settings with PowerShell.

  1. Brian 11 years ago

    The script changes the local PCs DNS and not the computer designated in the commandline.

  2. Achim 10 years ago

    Hi Brian,

    you must change line 32 to $Networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Computer -Filter ‘IPenabled=True’


  3. Matt 8 years ago

    Great script, thanks very much Sitaram.

Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2023


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account