Saturday, June 1, 2013

Powershell: Change DNS ip addressess remotely on multiple computers

Note: All the testings are performed in lab environment, use them at your risk.

Recently we upgraded and replaced DNS server, due to this we had to modify/add extra Dns server IP addresses on servers (around 1200 statically assigned), When I received this activity I got one CSV file containing all the details, where I started activity but got fed up after manually editing 20-25 entries.

I have revised this script on my new blog (Update 1 September 2017)
Powershell: Change DNS IP addresses remotely on multiple computers using CIM & WMI

Here I built this script on my home lab environment, it resembles to my office network but all the name, hostnames and ip addresses are different.

In my lab network, I have at least 2 network cards; only one network card is used on the intranet (connected to switch) and on the entire server this network card is renamed as "vNetwork"

(Here WMI service is used to do modification, it can change IP on windows 2003 server as well also it doesn't require powershell to be installed on remote server) 

Now I wanted to change/replace DNS ip for this vNetwork named adapter only.

You might not have permission to run script you will need to verify script execution policy on the server you going to run this script, and modify policy as below screenshot. (Make sure you are running powershell as administrator)


Create a list of servers where dns ip need to be change, I created one and renamed it servers.txt which is kept on one of the server under "c:\list"

Below is the script, copy to notepad file, modify green highlighted area as per you convenience and just execute script. I have renamed that txt file name to change-dns.ps1 and kept on "c:\list" with servers list


$Computerlist = get-content "C:\list\servers.txt"
$dnsservers =@(,"")

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 {$_.netconnectionID -eq "vNetwork"}
        $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"


Anonymous said...

This worked well and was VERY helpful, thank you!

Only caveat is the script text is missing quotes for the first DNS IP address (it is correct in your screenshot).

Thanks again!

Anonymous said...

Does not work for me. Placed the missing quotes. But this is the error I get.

You cannot call a method on a null-valued expression.
At line:13 char:9
+ $change.SetDNSServerSearchOrder($DNSservers) | out-null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull