TransWikia.com

Change DNS with script

Super User Asked by Endy Tjahjono on December 5, 2021

I need to frequently change the DNS server address, and for now I do it by opening ‘network and sharing center’ – ‘local area connection’ – properties – ipv4 – and then type the DNS numbers.

Is there a faster way to do it? Can I do it with a batch file or a powershell script? Is there a built in console command to change DNS?

8 Answers

All the rest of these answers use netsh, which at least on the Windows 2012R2 machine I was working with this morning reported:

In future versions of Windows, Microsoft might remove the Netsh functionality for TCP/IP.

Microsoft recommends that you transition to Windows PowerShell if you currently use netsh to configure and manage TCP/IP.

... now, I'm sure that's not the case anytime soon, but... still, it made me go and check Powershell.

Here's the powershell route I took, and assumes a single physical interface. If you've got more, you'll need to take a look into picking the right interface :)

$interface = (Get-NetAdapter).ifIndex
Set-DnsClient -InterfaceIndex $interface -ConnectionSpecificSuffix myproject.mydomain.tld
Set-DnsClientServerAddress -InterfaceIndex $interface -ServerAddresses ("8.8.8.8","8.8.4.4","2001:4860:4860::8888","2001:4860:4860::8844")

The relevant documents (as of 2020-05-15) are:

Answered by JonTheNiceGuy on December 5, 2021

Recently (end of 2019) I had problem configuring static DNS servers for Linux. I was reading very much about that and there was no single solution that would work for 100%. Always there was other part of system that was changing the values. Then I was digging how really change DNS forever. And the solution has come in that script:

#!/usr/bin/env bash
f="/etc/resolv.conf"
chattr -i $f
rm -f $f
echo "nameserver 1.1.1.1" >> $f
echo "nameserver 8.8.8.8" >> $f
chattr +i $f

That did the trick for long. The most important thing here is to make file with proper body and then set the immutable flag for it (chattr +i).

Answered by pbies on December 5, 2021

Here is your new friend: QuickSetDNS, by NirSoft, amazing as usual.

screenshot

It also can be used in command line :) with these advantages over netsh:

  • easier syntax, in particular for setting the alternate server
  • automatically asks for privilege elevation


Just a few caveats:

  • supports only setting of IPv4, not of IPv6
    • since QuickSetDNS 1.30, setting IPv6 DNS servers is also supported ;)
  • in command line, the adapter UUID should be used, not the friendly name (e.g. "Local Area Connection")
    • since QuickSetDNS 1.21, connection names are also supported ;)

Answered by Gras Double on December 5, 2021

This answer is copied from XP1 here. If XP1 would like to post this answer, please do so and I will delete my answer.

Here is another way to change DNS by using WMIC (Windows Management Instrumentation Command-line).

The commands must be run as administrator to apply.

Clear DNS servers:

wmic nicconfig where (IPEnabled=TRUE) call SetDNSServerSearchOrder ()

Set 1 DNS server:

wmic nicconfig where (IPEnabled=TRUE) call SetDNSServerSearchOrder ("8.8.8.8")

Set 2 DNS servers:

wmic nicconfig where (IPEnabled=TRUE) call SetDNSServerSearchOrder ("8.8.8.8", "8.8.4.4")

Set 2 DNS servers on a particular network adapter:

wmic nicconfig where "(IPEnabled=TRUE) and (Description = 'Local Area Connection')" call SetDNSServerSearchOrder ("8.8.8.8", "8.8.4.4")

Another example for setting the domain search list:

wmic nicconfig call SetDNSSuffixSearchOrder ("domain.tld")

Answered by Nathan on December 5, 2021

Adding a fix to Logman's version for WinXP (sp3 hebrew), seems like it needs to remove 2 chars at the end so I added a "global" kind of fix for any other weird case.

:: Set primary and alternate DNS for IPv4 on Windows Server 2000/2003/2008 & Windows XP/Vista/7
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET adapterName=

FOR /F "tokens=* delims=:" %%a IN ('IPCONFIG ^| FIND /I "ETHERNET ADAPTER"') DO (
SET adapterName=%%a

REM Removes "Ethernet adapter" from the front of the adapter name
SET adapterName=!adapterName:~17!

REM WinXP Remove some weird trailing chars (don't know what they are)
FOR /l %%a IN (1,1,255) DO IF NOT "!adapterName:~-1!"==":" SET adapterName=!adapterName:~0,-1!

REM Removes the colon from the end of the adapter name
SET adapterName=!adapterName:~0,-1!
echo !adapterName!
GOTO:EOF
netsh interface ip set dns name="!adapterName!" static x.x.x.x primary
netsh interface ip add dns name="!adapterName!" x.x.x.x index=2
)

http://pastebin.com/9mbMR7sy

Answered by Sniffleh on December 5, 2021

Using a Powershell script in Windows 8 or 2012, you may set the values like this:

Set-DnsClientServerAddress -InterfaceAlias Wi-Fi -ServerAddresses "1.1.1.1","2.2.2.2"

Where wi-Fi is the name of the interface you're interested in. You may list the interfaces by running:

Get-NetAdapter

To reset DNS addresses and this use DHCP:

Set-DnsClientServerAddress -InterfaceAlias wi-fi -ResetServerAddresses

Go to this page to see a full description.

Note that the comandlets used here are not available in earlier versions, such as Windows 7.

Answered by Juanal on December 5, 2021

Also to use DNS addresses provided by the DHCP server:

netsh interface ipv4 set dns "Local Area Connection" dhcp

Answered by mmm on December 5, 2021

Primary DNS value:

netsh interface ipv4 set dns "Local Area Connection" static 192.168.0.2

Secondary value:

netsh interface ipv4 add dns "Local Area Connection" 192.168.0.3 index=2

Which works great IF the name of the connection is correct. If the name isn't "Local Area Connection" then it will not work. If you are running XP you need to change "ipv4" to "ip". IPv6 can be used too.

Set subnet mask, IP Address, and Gateway:

netsh interface ipv4 set address name="Local Area Connection" source=static addr=192.168.1.10 mask=255.255.255.0 gateway=192.168.0.1

To Find the network connection you can use ipconfig from the cmd line. But you can also use the following for an abbreviated ipconfig result:

ipconfig | find /I "Ethernet adapter"

using the above ipconfig cmd we can loop through the connection (source code) and set the dns servers:

:: Set primary and alternate DNS for IPv4 on Windows Server 2000/2003/2008 & 
:: Windows XP/Vista/7
@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET adapterName=

FOR /F "tokens=* delims=:" %%a IN ('IPCONFIG ^| FIND /I "ETHERNET ADAPTER"') DO (
SET adapterName=%%a

REM Removes "Ethernet adapter" from the front of the adapter name
SET adapterName=!adapterName:~17!

REM Removes the colon from the end of the adapter name
SET adapterName=!adapterName:~0,-1!

netsh interface ipv4 set dns name="!adapterName!" static 192.168.0.2 primary
netsh interface ipv4 add dns name="!adapterName!" 192.168.0.3 index=2
)

ipconfig /flushdns

:EOF

Answered by Logman on December 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP