Change DNS Servers Remotely with Powershell

At some point most admins will need to change a DNS server (or two) on their network.  This is an easy change for DHCP clients, but can be a real pain for statically assigned clients.  Below is a simple Powershell script that runs through a list a computer names and updates the DNS servers on the clients.

 

$servers = "SERVERA","SERVERB","CLIENT1"

foreach($server in $servers)

{

    Write-Host "Connect to $server..."

    $nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"}

    $newDNS = "192.168.1.200","192.168.1.201"

    foreach($nic in $nics)

    {

        Write-Host "`tExisting DNS Servers " $nic.DNSServerSearchOrder

        $x = $nic.SetDNSServerSearchOrder($newDNS)

        if($x.ReturnValue -eq 0)

        {

            Write-Host "`tSuccessfully Changed DNS Servers on " $server

        }

        else

        {

            Write-Host "`tFailed to Change DNS Servers on " $server

        }

    }

}
 

Simply change $servers to match your list of computers.  What's that you say?  Don't feel like typing out a comma separated list of computers?  Change line 1 to $servers = Get-Content C:\PathToFile\computers.txt to feed in a list of computers from a text file.

This has been tested on Windows Server 2003, Windows Server 2008, Windows Vista, Windows 7, and Windows XP.  The script does requires WMI access to the computers so be sure to add an exception to the firewall if needed.

Your rating: None Average: 4.4 (17 votes)