Mass remote changing of DNS-servers in network interface (NIC) settings in Windows

During upgrade of domain controllers or theirs addition\removal in Active Directory domain, there is always a need to change DNS-servers in network interfaces settings in Windows. On those computers or servers where you set network settings with DHCP - there is no problems. But on those where you configure settings manually, you will have to change them manually also. It is long and inconvinient.

Here is script in Powershell which will make your task easier and make all automatically. You will need just to prepare a list of servers.

$computerlist = "server1.domain.local",
				"server3.domain.local" #Note that there is no comma at the end of this string
$dnsservers =@("","") #The list of new DNS servers
foreach ($computername in $computerlist) { 
    $result =  get-wmiobject win32_pingstatus -filter "address='$computername'" 
    if ($result.statuscode -eq 0) { 
        $remoteNic = get-wmiobject -class win32_networkadapter -computer $computername | where-object {$_.netconnectionstatus -ne $null} 
        $index = $remotenic.index 
        $DNSlist = $(get-wmiobject win32_networkadapterconfiguration -computer $computername -Filter ‘IPEnabled=true’ | where-object {$_.index -eq $index}).dnsserversearchorder 
        $priDNS = $DNSlist | select-object -first 1 
        Write-host "Changing DNS IP's on $computername" -b "Yellow" -foregroundcolor "black" 
        $change = get-wmiobject win32_networkadapterconfiguration -computer $computername | where-object {$_.index -eq $index} 
        $change.SetDNSServerSearchOrder($DNSservers) | out-null 
        $changes = $(get-wmiobject win32_networkadapterconfiguration -computer $computername -Filter ‘IPEnabled=true’ | where-object {$_.index -eq $index}).dnsserversearchorder 
        Write-host "$computername's Nic1 Dns IPs $changes" 
    else { 
        Write-host "$Computername is down cannot change IP address" -b "Red" -foregroundcolor "white" 

Remembeer that user under which you will run the script, must have administrative priviliges on these remote servers!

script (en), Windows (EN), powershell (en)

  • Hits: 10635
Add comment

Related Articles