87

How can I set my DNS settings using the command-prompt or bat file at windows 8

I tried this:

netsh interface ip set dns name="Local Area Connection" source=static addr=none

but not worked.

chue x
  • 18,573
  • 7
  • 56
  • 70
Zuhair Taha
  • 2,808
  • 2
  • 35
  • 33

9 Answers9

93

First, the network name is likely "Ethernet", not "Local Area Connection". To find out the name you can do this:

netsh interface show interface

Which will show the name under the "Interface Name" column (shown here in bold):

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Connected      Dedicated        Ethernet

Now you can change the primary dns (index=1), assuming that your interface is static (not using dhcp):

netsh interface ipv4 add dnsserver "Ethernet" address=192.168.x.x index=1

2018 Update - The command will work with either dnsserver (singular) or dnsservers (plural). The following example uses the latter and is valid as well:

netsh interface ipv4 add dnsservers "Ethernet" address=192.168.x.x index=1
chue x
  • 18,573
  • 7
  • 56
  • 70
  • It worked, thank you! Do you know how to set the DNS to "automatic settings" ? – patricK Mar 25 '14 at 18:02
  • 1
    @Patrick: See [my answer](http://stackoverflow.com/a/29598445/3131693). Put `dhcp` in place of `static` in second command. – Adarsh Rajput Apr 16 '15 at 07:12
  • 3
    @chue-x Contrary to your statement about what's "likely", it is _extremely_ common for Windows to assign the name "Local Area Connection" to a network connection. – Ross Presser Aug 20 '15 at 16:10
  • 1
    @RossPresser That was true for WinXP, Vista and 7 (not sure about earlier versions) but since Win8 that was changed to Ethernet. – Pixy Jul 31 '17 at 17:12
  • 3
    From the built-in command help, the canonical name is "dnsservers". Though, you can input only "dns", "dnsse"... or even just "d". Similarly, "ip" is a shorthand for "ipv4". – Gras Double May 31 '18 at 00:48
  • These worked for me: `netsh interface ipv4 set dnsserver "Ethernet0" static address=208.67.222.222` and `netsh interface ipv4 set dnsserver "Ethernet0" address=208.67.222.222 index=1` – Shayan Dec 26 '18 at 17:20
33

To change DNS to automatic via command, you can run the following command:

netsh interface ip set dns "Local Area Connection" dhcp
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Marcos Cassiano
  • 817
  • 8
  • 12
25

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")
Nathan
  • 8,093
  • 8
  • 50
  • 76
XP1
  • 6,910
  • 8
  • 54
  • 61
  • 4
    This is the best answer. Everyone else proposes to **mess** with interface names like _"Local Area Connection"_ as if they are required. – uvsmtid Aug 18 '16 at 02:20
  • I agree with @uvsmtid, this is the best answer. I started working on a much more complicated batch script and figured there had to be a better way. thanks! – jacktrader Jun 28 '18 at 15:56
  • 2
    For powershell replace `where (IPEnabled=TRUE)` with `where "IPEnabled=TRUE"`. – ndemou Nov 27 '18 at 16:53
  • As in 2023, WMIC is deprecated https://learn.microsoft.com/en-us/windows/whats-new/deprecated-features – user2956477 Jun 23 '23 at 05:16
19

There are little difference in command of adding AND changing DNS-IPs:

To Add:

Syntax:
   netsh interface ipv4 add dnsserver "Network Interface Name" dns.server.ip index=1(for primary)2(for secondary)
Eg:
   netsh interface ipv4 add dnsserver "Ethernet" 8.8.8.8 index=1
  • Here, to know "Network Interface Name", type command netsh interface show interface
  • 8.8.8.8 is Google's recursive DNS server, use it, if your's not working

To Set/Change: (as OP asked this)

Syntax:
   netsh interface ipv4 set dnsservers "Network Interface Name" static dns.server.ip primary
Eg:
   netsh interface ipv4 set dnsservers "Wi-Fi" static 8.8.4.4 primary
   netsh interface ipv4 set dnsservers "Wi-Fi" dhcp
  • Last parameter can be none:disable DNS, both:set for primary and secondary DNS both, primary: for primary DNS only. You can notice here we are not using index-parameter as we did in adding DNS.

  • In the place of static you can type dhcp to make DNS setting automatic, but further parameter will not be required.


Note: Tested in windows 8,8.1 & 10.

Adarsh Rajput
  • 1,246
  • 14
  • 24
  • 2
    The `primary` parameter at the end has nothing to do with primary/secondary DNS server. This parameter is used to control where the DNS client will try to dynamically register its name (`none`: do not register; `primary`: register at the primary DNS sufix; `both`: register at both, the primary DNS sufix and the connection-specific suffix). See `netsh interface ipv4 set dnsservers /?` for details. – stackprotector Mar 09 '21 at 13:25
17

I wrote this script for switching DNS servers of all currently enabled interfaces to specific address:

@echo off

:: Google DNS
set DNS1=8.8.8.8
set DNS2=8.8.4.4

for /f "tokens=1,2,3*" %%i in ('netsh int show interface') do (
    if %%i equ Enabled (
        echo Changing "%%l" : %DNS1% + %DNS2%
        netsh int ipv4 set dns name="%%l" static %DNS1% primary validate=no
        netsh int ipv4 add dns name="%%l" %DNS2% index=2 validate=no
    )
)

ipconfig /flushdns

:EOF
Fleshgrinder
  • 15,703
  • 4
  • 47
  • 56
Meow
  • 4,341
  • 1
  • 18
  • 17
  • Cool. I have improved on your answer by adding command param and filtering: https://gist.github.com/n074v41l4bl34u/e237c0f199b2bb68bf1c Any suggestions why my 'skip=2' option in FOR command is not working? – TermoTux Mar 20 '16 at 19:42
15

None of the answers are working for me on Windows 10, so here's what I use:

@echo off

set DNS1=8.8.8.8
set DNS2=8.8.4.4
set INTERFACE=Ethernet

netsh int ipv4 set dns name="%INTERFACE%" static %DNS1% primary validate=no
netsh int ipv4 add dns name="%INTERFACE%" %DNS2% index=2

ipconfig /flushdns

pause

This uses Google DNS. You can get interface name with the command netsh int show interface

MrVaykadji
  • 401
  • 4
  • 9
3

Batch file for setting a new dns server

@echo off
rem usage: setdns <dnsserver> <interface>
rem default dsnserver is dhcp
rem default interface is Wi-Fi
set dnsserver="%1"
if %dnsserver%=="" set dnsserver="dhcp"
set interface="%2"
if %interface%=="" set interface="Wi-Fi"
echo Showing current DNS setting for interface a%interface%
netsh interface ipv4 show dnsserver %interface%
echo Changing dnsserver on interface %interface% to %dnsserver%
if %dnsserver% == "dhcp" netsh interface ipv4 set dnsserver %interface% %dnsserver%
if NOT %dnsserver% == "dhcp" netsh interface ipv4 add dnsserver %interface% address=%dnsserver% index=1
echo Showing new DNS setting for interface %interface%
netsh interface ipv4 show dnsserver %interface%
BSalita
  • 8,420
  • 10
  • 51
  • 68
2

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 ;)
Gras Double
  • 15,901
  • 8
  • 56
  • 54
1

Now you can change the primary dns (index=1), assuming that your interface is static (not using dhcp)

You can set your DNS servers statically even if you use DHCP to obtain your IP address.

Example under Windows 7 to add two DN servers, the command is as follows:

netsh interface ipv4 add dns "Local Area Connection" address=192.168.x.x index=1 netsh interface ipv4 add dns "Local Area Connection" address=192.168.x.x index=2