88

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.

1
  • 2
    First of all, run cmd as administrator.
    – Jet
    Jul 23, 2014 at 11:14

9 Answers 9

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
6
  • It worked, thank you! Do you know how to set the DNS to "automatic settings" ?
    – patricK
    Mar 25, 2014 at 18:02
  • 1
    @Patrick: See my answer. Put dhcp in place of static in second command. Apr 16, 2015 at 7: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. Aug 20, 2015 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, 2017 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". May 31, 2018 at 0:48
33

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

netsh interface ip set dns "Local Area Connection" dhcp
26

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")
4
  • 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, 2016 at 2: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, 2018 at 15:56
  • 2
    For powershell replace where (IPEnabled=TRUE) with where "IPEnabled=TRUE".
    – ndemou
    Nov 27, 2018 at 16:53
  • As in 2023, WMIC is deprecated learn.microsoft.com/en-us/windows/whats-new/deprecated-features Jun 23 at 5: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.

1
  • 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. Mar 9, 2021 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
1
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

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%
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 ;)
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.