Share via


Setting Google DNS with powershell

My local ISP's router won't let me modify dns settings in it's dhcp table. (no drew Joy), now I'm going to have to add a separate router.. while I waiting for my router, I created the following powershell code to point my boxes to google dns.

The  following PS code will:

  1. look for a nic with an active gateway
  2. apply google dns server definitions for both ipv4 and ipv6 

Tested on Windows 8.1, 10, W2k12R2, W2k16

#requires admin ps (changing dns), Powershell 3+ (use $PSVersionTable to PS version)

#CODE

  1 2 3 4 5 6 7 8 910
 # Show current settingsGet-DnsClientServerAddress# look for nic w/ gateway$GWNic=(Get-NetIPConfiguration -All |Where {$_.IPv4DefaultGateway -ne $null}).interfaceindex# set dns servers to both ipv4/ipv6, reference https://developers.google.com/speed/public-dns/docs/usingSet-DnsClientServerAddress -InterfaceIndex $GWNic -ServerAddresses ("8.8.8.8","8.8.4.4","2001:4860:4860::8888","2001:4860:4860::8844")# ConfirmationGet-DnsClientServerAddress

#

 

Updated the following for CloudFlare DNS

# Show current settings
Get-DnsClientServerAddress
# look for nic w/ gateway
$GWNic=(Get-NetIPConfiguration -All |Where {$_.IPv4DefaultGateway -ne $null}).interfaceindex

# set dns servers to both ipv4/ipv6, #Reference: https://blog.cloudflare.com/dns-resolver-1-1-1-1/
Set-DnsClientServerAddress -InterfaceIndex $GWNic -ServerAddresses ("1.1.1.1","1.0.0.1","2606:4700:4700::1111","2606:4700:4700::1001")

# Confirmation
Get-DnsClientServerAddress