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 (7 votes)

Thank you!!!

Works like a charm!!! Saved ton of time...

THanks

Thanks it worked !! i checked

Thanks it worked !!

i checked from 2008 to 2003 servers

for more info about powershell ...please visit below

Running Windows PowerShell Scripts
http://technet.microsoft.com/en-us/library/ee176949.aspx

Glad that worked for you.

Glad that worked for you.

Thanks for the script

Thanks, here is another way to change the ip info and dns servers remotely using psexec and netsh:
http://itswapshop.com/content/how-set-or-change-ip-addresses-gateways-subnet-masks-and-dns-servers-remotely-windows

Great Script!!

Been trying to use psexec but Powershell comes right through for Visa, 7, etc.

THANKS!!