Skip to content

Instantly share code, notes, and snippets.

@rikonor
Created April 21, 2016 18:26
Show Gist options
  • Save rikonor/50855c98ae1f6f3c7b79c215b1e922bb to your computer and use it in GitHub Desktop.
Save rikonor/50855c98ae1f6f3c7b79c215b1e922bb to your computer and use it in GitHub Desktop.
$desiredPrimary = "127.0.0.1"
# Get current DNS Servers
$foo = Get-DnsClientServerAddress | where { $_.InterfaceAlias -eq "Ethernet"} | ? {$_.AddressFamily -eq 2 }
$dnsServers = $foo.ServerAddresses
# Stop if primary is already localhost
if ($dnsServers[0].Equals($desiredPrimary)) {
echo "Primary DNS Server is already set to 127.0.0.1"
exit 0
}
# Prepend localhost as Primary DNS Server
$newDNSServersList = New-Object 'System.Collections.Generic.List[string]'
$newDNSServersList.Add($desiredPrimary)
# Add the rest
ForEach ($dnsServer in $dnsServers) {
$newDNSServersList.Add($dnsServer)
}
# Set the new DNS Servers list
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses $newDNSServersList
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment