[windows-8] How can I set / change DNS using the command-prompt at windows 8

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.

This question is related to windows-8 dns command-prompt

The answer is


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

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


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%

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")

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.


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


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

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

netsh interface ip set dns "Local Area Connection" dhcp

Examples related to windows-8

How to install and use "make" in Windows? How do I create a shortcut via command-line in Windows? Signtool error: No certificates were found that met all given criteria with a Windows Store App? How to open/run .jar file (double-click not working)? Forbidden: You don't have permission to access / on this server, WAMP Error How can I set / change DNS using the command-prompt at windows 8 "Unable to launch the IIS Express Web server" error Can't ping a local VM from the host Viewing localhost website from mobile device How to download/upload files from/to SharePoint 2013 using CSOM?

Examples related to dns

How is VIP swapping + CNAMEs better than IP swapping + A records? ping: google.com: Temporary failure in name resolution What's the source of Error: getaddrinfo EAI_AGAIN? How do I solve the "server DNS address could not be found" error on Windows 10? Creating self signed certificate for domain and subdomains - NET::ERR_CERT_COMMON_NAME_INVALID How to force DNS refresh for a website? ssh: Could not resolve hostname [hostname]: nodename nor servname provided, or not known How to filter wireshark to see only dns queries that are sent/received from/by my computer? How can I list ALL DNS records? How to redirect DNS to different ports

Examples related to command-prompt

CMD command to check connected USB devices How do I kill the process currently using a port on localhost in Windows? PowerShell The term is not recognized as cmdlet function script file or operable program open program minimized via command prompt How to connect to SQL Server from command prompt with Windows authentication How to see the proxy settings on windows? How do I type a TAB character in PowerShell? Batch file to split .csv file Aliases in Windows command prompt Change all files and folders permissions of a directory to 644/755