How to change/update DNS settings with Powershell on network computers

I have few computers on network that are required to have a static ip address for certain reasons. I have changed DNS server settings and now I have two options:

  1. Go to each of the machines and change the settings manually
  2. Use script and do it easily from my desk over network

Since SysAdmin supposed to be efficient and healthy lazy to use his time wisely all the time … I have decide for the second option – I have found this fancy PowerShell script that reads the computers IP or names from the text file and make the required changes on each of the computers.

Feel Free to use it (test it first before you run it on your network)

*************************************************************

 

$servers = C:ADMINcomputers.txt
foreach($server in $servers)
{
Write-Host "Connect to $server..."
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"}
$newDNS = "10.0.4.16","10.0.4.15"
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
}
}
}

Hope it helps:-)