Solved How to set DNS over HTTPS via command prompt?


Genshii

Member
Local time
8:45 PM
Posts
5
Visit site
OS
Windows 11
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

  • OS
    Windows 11

abactuon

Well-known member
Member
VIP
Local time
7:45 AM
Posts
294
Visit site
OS
Microsoft Windows 11 Home
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

  • OS
    Microsoft Windows 11 Home
    Computer type
    Laptop
    Manufacturer/Model
    ASUS TUF Dash F15 FX516PM_FX516PM
    CPU
    Intel Core i7-11370H
    Motherboard
    ASUS FX516PM
    Memory
    Micron 4ATF1G64HZ-3G2E2/8G + Patriot PSD416G320081S
    Graphics Card(s)
    NVIDIA GeForce RTX 3060 Laptop
    Sound Card
    Edifier 800BT Plus
    Monitor(s) Displays
    LM156LF-2F03
    Screen Resolution
    1920 x 1080
    Hard Drives
    1. PCIe Gen 4 KINGSTON SNV2S1000G
    2. PCIe Gen 3 SK Hynix HFM512GD3JX013N
    PSU
    200W
    Mouse
    Corsair KATAR PRO XT
    Internet Speed
    70 Mbps
    Browser
    Firefox
    Antivirus
    Microsoft Defender Antivirus

Genshii

Member
Thread Starter
Local time
8:45 PM
Posts
5
Visit site
OS
Windows 11
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

  • OS
    Windows 11

abactuon

Well-known member
Member
VIP
Local time
7:45 AM
Posts
294
Visit site
OS
Microsoft Windows 11 Home
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

  • OS
    Microsoft Windows 11 Home
    Computer type
    Laptop
    Manufacturer/Model
    ASUS TUF Dash F15 FX516PM_FX516PM
    CPU
    Intel Core i7-11370H
    Motherboard
    ASUS FX516PM
    Memory
    Micron 4ATF1G64HZ-3G2E2/8G + Patriot PSD416G320081S
    Graphics Card(s)
    NVIDIA GeForce RTX 3060 Laptop
    Sound Card
    Edifier 800BT Plus
    Monitor(s) Displays
    LM156LF-2F03
    Screen Resolution
    1920 x 1080
    Hard Drives
    1. PCIe Gen 4 KINGSTON SNV2S1000G
    2. PCIe Gen 3 SK Hynix HFM512GD3JX013N
    PSU
    200W
    Mouse
    Corsair KATAR PRO XT
    Internet Speed
    70 Mbps
    Browser
    Firefox
    Antivirus
    Microsoft Defender Antivirus

TairikuOkami

Brony
Power User
VIP
Local time
1:45 AM
Posts
684
Visit site
OS
Windows 11 Home
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

  • OS
    Windows 11 Home
    CPU
    AMD Ryzen 5 3600 (07/19)
    Motherboard
    MSI B450 TOMAHAWK 7C02v1E (07/19)
    Memory
    4x 8GB ADATA XPG GAMMIX D10 DDR4 3200MHz CL16
    Graphics Card(s)
    MSI Radeon RX 580 ARMOR 8G OC @48FPS (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 Wired Desktop 320K Keyboard (04/22)
    Mouse
    HP Wireless Silent 280M Mouse (05/21)
    Internet Speed
    400/40 Mbps via RouterOS (05/21) + TCP Optimizer
    Browser
    Edge with Neeva (No FB/Google) + Brave for YouTube + LibreWolf for FB
    Antivirus
    NoAV + Binisoft WFC + NextDNS
    Other Info
    Headphones: Sennheiser RS170 (09/10)

Genshii

Member
Thread Starter
Local time
8:45 PM
Posts
5
Visit site
OS
Windows 11
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

  • OS
    Windows 11

infinity

Active member
VIP
Local time
8:45 AM
Posts
109
Visit site
OS
Win 11
tomatically 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 Cloudflar

Hi there, I came across your useful script and wanted to ask - is this automatically applied to all network interfaces (Ethernet, WiFi networks) that I have without having to specify the network name if I'm just using cloudflare DNS?
 

My Computer

System One

  • OS
    Win 11
    Computer type
    PC/Desktop
    CPU
    Ryzen 3600
    Motherboard
    MSI B450 Tomahawk Max
    Memory
    GSkill RipJawz 16Gbx2
    Graphics Card(s)
    GTX960
    Screen Resolution
    2560x1440
    Hard Drives
    WD750 SSD 1TB
    PSU
    650w FSP

Genshii

Member
Thread Starter
Local time
8:45 PM
Posts
5
Visit site
OS
Windows 11
Hi there, I came across your useful script and wanted to ask - is this automatically applied to all network interfaces (Ethernet, WiFi networks) that I have without having to specify the network name if I'm just using cloudflare DNS?

Yeah, it's applied to all network interfaces regardless of the actual network you're connecting to.
 

My Computer

System One

  • OS
    Windows 11

pmgcosta

New member
Local time
12:45 AM
Posts
1
Visit site
OS
WIndows 11
Hello, using the script i have a problem, the DNS settings of my Bluetooth connection are changed.

I have converted it to powershell, that allow me to filter only Wireless and Wired networks adapters.


Powershell:
$i = Get-NetAdapter -Physical
$i | Get-DnsClientServerAddress -AddressFamily IPv4 | Set-DnsClientServerAddress -ServerAddresses '1.1.1.1', '8.8.8.8'
$i | Get-DnsClientServerAddress -AddressFamily IPv6 | Set-DnsClientServerAddress -ServerAddresses '2606:4700:4700::1111', '2001:4860:4860::8888'
$i | ForEach-Object {
$s1 = 'HKLM:System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\' + $_.InterfaceGuid + '\DohInterfaceSettings\Doh\1.1.1.1'; New-Item -Path $s1 -Force | New-ItemProperty -Name "DohFlags" -Value 1 -PropertyType Qword
$s2 = 'HKLM:System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\' + $_.InterfaceGuid + '\DohInterfaceSettings\Doh\8.8.8.8'; New-Item -Path $s2 -Force  | New-ItemProperty -Name "DohFlags" -Value 1 -PropertyType Qword
$s3 = 'HKLM:System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\' + $_.InterfaceGuid + '\DohInterfaceSettings\Doh6\2606:4700:4700::1111'; New-Item -Path $s3 -Force | New-ItemProperty -Name "DohFlags" -Value 1 -PropertyType Qword
$s4 = 'HKLM:System\CurrentControlSet\Services\Dnscache\InterfaceSpecificParameters\' + $_.InterfaceGuid + '\DohInterfaceSettings\Doh6\2001:4860:4860::8888'; New-Item -Path $s4 -Force  | New-ItemProperty -Name "DohFlags" -Value 1 -PropertyType Qword
}
Clear-DnsClientCache;
 

My Computer

System One

  • OS
    WIndows 11

Latest Tutorials

Latest Support Threads

Top Bottom