Drew Robinson's Blog

Azure, Powershell and Security things

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
 9
10
# 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://developers.google.com/speed/public-dns/docs/using
Set-DnsClientServerAddress -InterfaceIndex $GWNic -ServerAddresses ("8.8.8.8","8.8.4.4","2001:4860:4860::8888","2001:4860:4860::8844")

# Confirmation
Get-DnsClientServerAddress