Chang DNS and WINS servers on multiple servers using Powershell

From roonics
Jump to navigation Jump to search

Sometimes you may need to change the DNS and WINS (if you still use it) on multiple servers which can be very time consuming, to do this on multiple servers with a script follow the below.

1. Export a list of the servers you wish to change the DNS and WINS servers on and save it as a txt file in the same location as the script called "computers.txt" as below:

server01
server02
server03
server04

2. Copy and paste the below script in to Notepad and save it as a ps1 file:

$servers = Get-Content computers.txt

foreach($server in $servers)

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

    foreach($nic in $nics)

    {
        # EDIT NEW DNS SERVERS HERE
	$newDNS = "10.10.10.1","10.10.10.2"
                
        Write-Host "`tExisting DNS Servers " $nic.DNSServerSearchOrder
        Write-Host "`tExisting WINS Primary Server" $nic.WINSPrimaryServer
        Write-Host "`tExisting WINS Secondary Server" $nic.WINSSecondaryServer

        $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
        }
        
        # EDIT NEW WINS SERVERS HERE
        $y = $nic.SetWINSServer("10.10.10.3","10.10.10.4")
        if($y.ReturnValue -eq 0)
        {
            Write-Host "`tSuccessfully Changed WINS Servers on " $server
        }
        else
        {
            Write-Host "`tFailed to Change WINS Servers on " $server
        }

    }

}

3. Now run the script.