Solved How to set DNS over HTTPS via command prompt?

Genshii

Member
Local time
7:34 AM
Posts
4
Is there a way to set DNS over HTTPS via netsh or wmic? I know you can add additional DoH templates via netsh, and I know you can set DNS, but I can't figure out if there's a way to actually set DNS + activate DoH without going into settings.

In other words, I'd like to do this via command prompt:

1630961569300.png

Thank you in advance.
 
Last edited:

My Computer

System One

  • Operating System
    Windows 11

abactuon

Active member
Member
Local time
6:34 PM
Posts
117
I did it for Windows 11, but did not check. Not yet Windows 11.

# Cloudflare. +Block malware and adult content
$PrimaryDNS = '1.0.0.3'
$SecondaryDNS = '1.1.1.3'

# DNS server registration with DOH support
Add-DnsClientDohServerAddress -ServerAddress $PrimaryDNS -DohTemplate 'https://family.cloudflare-dns.com/dns-query' -AllowFallbackToUdp $False -AutoUpgrade $True
Add-DnsClientDohServerAddress -ServerAddress $SecondaryDNS -DohTemplate 'https://family.cloudflare-dns.com/dns-query' -AllowFallbackToUdp $False -AutoUpgrade $True
# All network adapters
$PhysAdapter = Get-NetAdapter -Physical
$PhysAdapter | Get-DnsClientServerAddress -AddressFamily IPv4 | Set-DnsClientServerAddress -ServerAddresses ($PrimaryDNS, $SecondaryDNS)
 

My Computer

System One

  • Operating System
    Microsoft Windows 10 Home (x64)
    Computer type
    Laptop
    Manufacturer/Model
    MSI GF65 Thin 9SD
    CPU
    Intel Core i5-9300H
    Motherboard
    MSI MS-16W1
    Memory
    2 x Kingston KHX2666C15S4/16G
    Graphics Card(s)
    GeForce GTX 1660 Ti
    Sound Card
    Intel Cannon Lake-H/S - cAVS
    Monitor(s) Displays
    AU Optronics B156HAN13.0
    Screen Resolution
    1920 x 1080
    Hard Drives
    1. Noname 128GB SSD
    2. SAMSUNG MZVLB512HAJQ
    PSU
    180W
    Mouse
    HP USB Fingerprint Mouse
    Internet Speed
    50 Mbps
    Browser
    Firefox

Genshii

Member
Thread Starter
Local time
7:34 AM
Posts
4
Unfortunately that doesn't actually enable DoH. It sets the primary/secondary DNS as expected, but I still have to go into settings and manually toggle on DoH.
 

My Computer

System One

  • Operating System
    Windows 11

abactuon

Active member
Member
Local time
6:34 PM
Posts
117
Does it work ?

Once you know your Windows install has our DoH client, we need to activate it. You can do that by:

Opening the Registry Editor
Navigate to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters registry key
Create a new DWORD value named “EnableAutoDoh”
Set its value to 2
 

My Computer

System One

  • Operating System
    Microsoft Windows 10 Home (x64)
    Computer type
    Laptop
    Manufacturer/Model
    MSI GF65 Thin 9SD
    CPU
    Intel Core i5-9300H
    Motherboard
    MSI MS-16W1
    Memory
    2 x Kingston KHX2666C15S4/16G
    Graphics Card(s)
    GeForce GTX 1660 Ti
    Sound Card
    Intel Cannon Lake-H/S - cAVS
    Monitor(s) Displays
    AU Optronics B156HAN13.0
    Screen Resolution
    1920 x 1080
    Hard Drives
    1. Noname 128GB SSD
    2. SAMSUNG MZVLB512HAJQ
    PSU
    180W
    Mouse
    HP USB Fingerprint Mouse
    Internet Speed
    50 Mbps
    Browser
    Firefox

TairikuOkami

Well-known member
Member
VIP
Local time
1:34 PM
Posts
163
Location
Trnava, SK
You need to know network adapter's ID, but that is randomly assigned, so maybe via a script? :think:

Code:
rem Allow DoH
reg add "HKLM\System\CurrentControlSet\Services\Dnscache\Parameters" /v "EnableAutoDoh" /t REG_DWORD /d "2" /f

rem Add custom DNS
netsh dns add encryption server=94.140.14.15 dohtemplate=https://dns-family.adguard.com/dns-query autoupgrade=yes udpfallback=no
netsh dns add encryption server=94.140.15.16 dohtemplate=https://dns-family.adguard.com/dns-query autoupgrade=yes udpfallback=no

rem Set DNS on DHCP or on a static IP based on MAC address
wmic nicconfig where DHCPEnabled=TRUE call SetDNSServerSearchOrder ("94.140.14.15","94.140.15.16")
wmic nicconfig where macaddress="00:D8:61:6E:E8:C5" call SetDNSServerSearchOrder ("94.140.14.15","94.140.15.16")

rem Enable DoH
reg add "HKLM\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\{0bd56988-9769-4d69-9e1b-9c15fc223590}\DohInterfaceSettings\Doh\94.140.14.15" /v "DohFlags" /t REG_QWORD /d "1" /f
reg add "HKLM\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\{0bd56988-9769-4d69-9e1b-9c15fc223590}\DohInterfaceSettings\Doh\94.140.15.16" /v "DohFlags" /t REG_QWORD /d "1" /f
This worked for me (bypassing UAC), when I entered mine ID.

capture_09072021_220005.jpg
 

My Computer

System One

  • Operating System
    Windows 11 Home
    CPU
    AMD Ryzen 5 3600 (07/19)
    Motherboard
    MSI B450 TOMAHAWK 7C02v1H6 (07/19)
    Memory
    4x 8GB ADATA XPG GAMMIX D10 DDR4 3200MHz CL16
    Graphics Card(s)
    MSI Radeon RX 580 ARMOR 8G OC (08/19)
    Sound Card
    Creative Sound Blaster Z (11/16)
    Monitor(s) Displays
    24" AOC G2460VQ6 (01/19)
    Screen Resolution
    1920×1080@75Hz + FreeSync (DisplayPort)
    Hard Drives
    ADATA XPG GAMMIX S11 Pro SSD 512GB (07/19)
    PSU
    Seasonic M12II-520 80 Plus Bronze (11/16)
    Case
    Lian Li PC-7NB + 3x Noctua NF-S12A FLX@700rpm (11/16)
    Cooling
    CPU Cooler Noctua NH-U12S@700rpm
    Keyboard
    HP Pavilion Wireless Keyboard 600 (05/21)
    Mouse
    HP Wireless Silent 280M Mouse (05/21)
    Internet Speed
    300/30 Mbps via RouterOS (05/21) + TCP Optimizer
    Browser
    Microsoft Edge
    Antivirus
    None
    Other Info
    Headphones: Sennheiser RS170 (09/10) + Software: https://tinyurl.com/7hkjyhsj

Genshii

Member
Thread Starter
Local time
7:34 AM
Posts
4
Appreciate the help. I was able to write a batch script that automatically sets given DNS servers and enables DoH on every relevant network interface for both IPv4 and IPv6. We can get the interface IDs and indexes from wmic nic and use that to set DNS with netsh. For my use case, I just want to use the default Cloudflare DNS servers, so it assumes that your selected DNS servers already have the proper DoH templates added.

Also, I didn't end up needing to add EnableAutoDoh to registry. I don't have that key at all and DoH works fine.

Code:
@echo off

rem Set the DNS servers to be applied to each interface.
set IPv4PrimaryDNS=1.1.1.1
set IPv4SecondaryDNS=1.0.0.1
set IPv6PrimaryDNS=2606:4700:4700::1111
set IPv6SecondaryDNS=2606:4700:4700::1001

rem Checks for administrative permissions.
net.exe session 1>NUL 2>NUL || (echo This script requires administrative permissions. Please run as administrator. & pause & exit /B 1)

echo Using the following DNS servers:
echo IPv4:
echo Primary - %IPv4PrimaryDNS%
echo Secondary - %IPv4SecondaryDNS%
echo/
echo IPv6:
echo Primary - %IPv6PrimaryDNS%
echo Secondary - %IPv6SecondaryDNS%
echo/

rem Clears existing DoH settings.
reg delete "HKLM\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters" /f 1>NUL
echo Cleared any existing DoH settings.
echo/

rem The following for loops get a given interface's InterfaceIndex and GUID. We use the InterfaceIndex to set DNS, and the GUID to set DoH in the registry.
rem We only care about network interfaces that have a GUID.
for /f %%X in ('wmic nic where "GUID!=NULL" Get InterfaceIndex /value') do (
    rem We have to use a second for loop to remove the extra carrige returns from wmic output.
    rem InterfaceIndex is stored at %%I.
    for /f "tokens=1* delims==" %%H in ("%%X") do (
        for /f %%X in ('wmic nic where "InterfaceIndex=%%I" Get GUID /value') do (
            rem GUID is stored at %%G.
            for /f "tokens=1* delims==" %%F in ("%%X") do (

                rem Prints the name of the interface being modified.
                for  /f "tokens=*" %%X in ('wmic nic where "InterfaceIndex=%%I" Get NetConnectionID /value') do (
                    for /f "tokens=1* delims==" %%B in ("%%X") do (
                        for  /f "tokens=*" %%X in ('wmic nic where "InterfaceIndex=%%I" Get Name /value') do (
                            for /f "tokens=1* delims==" %%M in ("%%X") do echo %%C ^(%%N^):
                        )
                    )
                )
                echo/

                rem Clears existing DNS servers.
                netsh interface ipv4 set dnsservers %%I dhcp 1>NUL
                echo Cleared any existing IPv4 DNS servers.
                netsh interface ipv6 set dnsservers %%I dhcp 1>NUL
                echo Cleared any existing IPv6 DNS servers.
                echo/

                netsh interface ipv4 set dnsservers %%I static %IPv4PrimaryDNS% primary no 1>NUL
                echo Set primary IPv4 DNS server to: %IPv4PrimaryDNS%
                netsh interface ipv4 add dnsservers %%I %IPv4SecondaryDNS% index=2 no 1>NUL
                echo Set secondary IPv4 DNS server to: %IPv4SecondaryDNS%
                echo/

                netsh interface ipv6 set dnsservers %%I static %IPv6PrimaryDNS% primary no 1>NUL
                echo Set primary IPv6 DNS server to: %IPv6PrimaryDNS%
                netsh interface ipv6 add dnsservers %%I %IPv6SecondaryDNS% index=2 no 1>NUL
                echo Set secondary IPv6 DNS server to: %IPv6SecondaryDNS%
                echo/

                reg add "HKLM\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\%%G\DohInterfaceSettings\Doh\%IPv4PrimaryDNS%" /v "DohFlags" /t REG_QWORD /d "1" /f 1>NUL
                reg add "HKLM\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\%%G\DohInterfaceSettings\Doh\%IPv4SecondaryDNS%" /v "DohFlags" /t REG_QWORD /d "1" /f 1>NUL
                echo Enabled DoH for IPv4.

                reg add "HKLM\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\%%G\DohInterfaceSettings\Doh6\%IPv6PrimaryDNS%" /v "DohFlags" /t REG_QWORD /d "1" /f 1>NUL
                reg add "HKLM\System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\%%G\DohInterfaceSettings\Doh6\%IPv6SecondaryDNS%" /v "DohFlags" /t REG_QWORD /d "1" /f 1>NUL
                echo Enabled DoH for IPv6.
                echo/
            )
         
        )
    )
)

ipconfig /flushdns 1>NUL
echo Flushed DNS.
echo/

pause
 
Last edited:

My Computer

System One

  • Operating System
    Windows 11
Top Bottom