Set DNS with PowerShell
using the netsh.exe
program to script changes to the network interfaces is a great way to automate configuring them. Changing DNS is simple:
# turn on DHCP assigned DNS serversnetsh int ip set address "Local Area Connection" dhcp# set a static DNS entrynetsh int ip set dns "Local Area Connection" static 192.168.1.1
A few notes:
- You would need to change
"Local Area Connection"
to the name of the connection you are working with. Though this is generally the default - it may just work in your case. The DNS server address would also need to be specific to your scenario. - Changing IP information usually requires elevated privileges, so make sure you are running PowerShell with elevated rights - by default Windows Vista and later launch PowerShell without elevating it. You will need to right click on it and choose "Run as admin".
$DC = read-host "Please enter a primary DNS"$Internet = read-host "Please enter secondary DNS"$dns = "$DC", "$Internet"
For powershell 4:
#I know this one is sloppy, but will get the work done.Set-DnsClientServerAddress -InterfaceIndex 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 17, 18, 19, 20 -ServerAddresses ($DNS)#I know this one is sloppy, but will get the work done.
For Powershell 1,2 and 3.
$Interface = Get-WmiObject Win32_NetworkAdapterConfiguration Write-Host "$(Get-Date -format T):Registering DNS $dns for $server" -ForegroundColor Green$Interface.SetDNSServerSearchOrder($dns)
I plucked these off a script I wrote actually yesterday for a client that needed to set the DNS range for some-odd 200 Computers since the DC was failing.
If you are interested I can give you the whole script.Point of the script is to run in a domain, and set every computer' DNS in the domain.
Edit, Made a Better version of the executing part (PS4+)
This script should not show any errors, if it does something went wrong. Unlike previous script for ps4, it would always output several errors.
$ErrorActionPreference = "Continue"#Set all DNS addresses needed.write-verbose -Verbose "Set all DNS addresses needed."$DC = "192.168.103.30"$Internet = "8.8.8.8" #Google$WorkRouter = "192.168.12.254"$HomeRouter = "10.0.0.1"$Possible = "192.168.1.1"$Possible2 = "192.168.0.1"#Combine addresseswrite-verbose -Verbose "Combining DNS addresses."$dns = "$DC", "$Internet", "$WorkRouter", "$HomeRouter", "$Possible", "$Possible2"#Set network adapter rangeswrite-verbose -Verbose "Setting network adapter ranges."#Get Network adapterswrite-Verbose -Verbose "Now checking available network adapters."$Net = Get-NetAdapter | select ifIndex | ft -a | Out-File -FilePath C:/Netadapter.txt$Net = "C:/Netadapter.txt"#Setting ranges to work with$Ranges = (Get-Content $Net) -creplace "ifIndex", "" -creplace "-", "" | foreach {$_.Trim()} | Where { $_ } | Sort #| out-file C:/Netadapter.txt#Execute DNS changewrite-Warning -Verbose "Now executing DNS change to all available network adapters."foreach ($range in $ranges) {Set-DnsClientServerAddress -InterfaceIndex $range -ServerAddresses ($DNS)}write-verbose -Verbose "DNS Settings have been altered."
This answer requires Powershell 4.
- Run
Get-DNSClientServerAddress
. In the output, look for the Interface Index of the adapter you want to change. You will need this in step 2. - In an elevated prompt, run
Set-DNSClientServerAddress –interfaceIndex ? –ServerAddresses ("8.8.8.8")
where?
is the Interface Index of the interface you want to change DNS server address for (8.8.8.8 is Google's DNS - always a good standby, but change this to the address of whatever DNS server you want). - If you want to change the interface back to 'Obtain DNS server address automatically', in an elevated prompt, run
Set-DnsClientServerAddress -InterfaceIndex ? -ResetServerAddresses
(Thanks to this blog for this tip), again substituting the Interface Index you looked up in step 1 for?
.