On both Windows 10 and Windows 11, you can change the DNS address of your computer via the Settings app. It is a pretty simple process but takes quite a lot of clicks & keystrokes and you need to know where to find the option to edit the DNS settings in the first place. If you want to speed up the process, you can use my PowerShell script to change the DNS IP address. When executed, it shows the current DNS addresses of all your network adapters and allows you to set a custom DNS address, both primary and secondary addresses.
PowerShell Script to Change DNS IP address (Primary & Secondary)
Here is the PowerShell script that allows you to change the DNS settings in Windows.
<#
Script Name: DNS Configuration Tool
Description: This script changes the DNS settings for all active network adapters in Windows.
Author: Bashkarla Vamsi
URL: https://windowsloop.com
#>
function Get-ValidIPAddress {
param (
[string]$prompt,
[bool]$allowEmpty = $false
)
do {
$ip = Read-Host -Prompt $prompt
if ($allowEmpty -and $ip -eq "") {
return $null
}
$ipValid = $ip -match '^\d{1,3}(\.\d{1,3}){3}$' -and ($ip.Split('.') | ForEach-Object {$_ -ge 0 -and $_ -le 255})
if (-not $ipValid) {
Write-Host "Invalid IP address. Please enter a valid IPv4 address or leave blank if optional." -ForegroundColor Red
}
} while (-not $ipValid)
return $ip
}
function DisplayCurrentDNS {
param (
[string]$description,
[System.Management.Automation.PSObject]$adapters
)
Write-Host "`n$description" -ForegroundColor Cyan
foreach ($adapter in $adapters) {
$dns = (Get-DnsClientServerAddress -InterfaceIndex $adapter.InterfaceIndex).ServerAddresses
$dnsString = if ($dns) { $dns -join ', ' } else { "Not Set" }
Write-Host "Adapter: $($adapter.Name) - DNS: $dnsString" -ForegroundColor Yellow
}
}
# Get all network adapters that are enabled
$adapters = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' }
# Display current DNS settings
DisplayCurrentDNS -description "Current DNS Settings:" -adapters $adapters
# Ask for Primary and Secondary DNS addresses
$primaryDNS = Get-ValidIPAddress -prompt 'Primary DNS'
$secondaryDNS = Get-ValidIPAddress -prompt 'Secondary DNS (leave blank if not needed)' -allowEmpty $true
# Set DNS servers for each adapter and display changes
foreach ($adapter in $adapters) {
Write-Host "`nSetting DNS for adapter: $($adapter.Name)" -ForegroundColor Green
$dnsSettings = @($primaryDNS) + @($secondaryDNS | Where-Object { $_ }) # Only add secondary if not null
try {
Set-DnsClientServerAddress -InterfaceIndex $adapter.InterfaceIndex -ServerAddresses $dnsSettings -ErrorAction Stop
Write-Host "DNS changed successfully for $($adapter.Name)" -ForegroundColor Green
} catch {
Write-Host "Failed to change DNS for $($adapter.Name)" -ForegroundColor Red
}
}
# Display new DNS settings
DisplayCurrentDNS -description "New DNS Settings:" -adapters $adapters
Using the Script to Update the DNS
Save the Script to Your Computer
First, click the Start button on your desktop, search for Notepad, and click Open.

In the Notepad window, copy (Ctrl + C) the script and paste (Ctrl + V) it into Notepad.
Click the File > Save As option.

Select a folder to save the file, set change-dns.ps1 as the “File name,” select All Files from the “Save as type” dropdown menu, and click Save.
Note: You must use .ps1 as the file extension.

Execute the Script
Note: You require administrator rights to modify DNS settings using the PowerShell script.
First, right-click on the Start button on the taskbar and select the Terminal (Admin) option. Windows 10 users can select PowerShell (Admin).

By default, PowerShell’s execution policy is restricted and will not allow you to execute scripts. To change that, run the below command. It sets the execution policy to RemoteSigned which lets us run our script.
Set-ExecutionPolicy RemoteSigned

Next, use the CD command as shown below to go to the folder where you saved the script.
cd "C:\path\to\script"

Now, run the following command to execute the script.
Note: If you saved the file with some other name, adjust the name in the command accordingly.
.\change-dns.ps1

As soon as you execute the script, it shows the current DNS addresses for all the network adapters on your computer and then prompts for a new primary DNS address.
Enter the primary DNS IP address (in IPv4) and press Enter. For instance, in my case, I want to switch my DNS from using OpenDNS to Google Public DNS. So, I entered the Google Public DNS address (8.8.8.8).

Next, enter the Secondary DNS IP address and press Enter.

That’s it. As soon as you press Enter, the PowerShell script changes the DNS address and shows the updated DNS of all the network adapters on your PC.

How Does The Script Work?
When you execute the script, it first triggers the DisplayCurrentDNS
function to show your current DNS settings for all active network adapters. Next, you are prompted to enter the primary and secondary DNS addresses which are validated using the Get-ValidIPAddress
function and are stored in the $primaryDNS
and $secondaryDNS
variables. Then, the script uses the Set-DnsClientServerAddress
cmdlet to apply the changes. It then immediately runs the DisplayCurrentDNS
again to display the modified DNS settings.
Microsoft documentation on using Set-DnsClientServerAddress cmdlet.
Limitations
The script only changes the DNS address for active network adapters. That means, if a network adapter is disabled, it will not modify its DNS settings.
Additionally, it will not validate whether the DNS is working or not.
Wrapping Up — A Simple PS Script to Better Manage DNS Settings
Compared to using the settings app, using my PowerShell script makes changing and updating DNS settings a breeze by eliminating the need to deal with clumsy UI. This is especially true when you are switching DNS frequently, testing network configurations, and managing multiple machines.
If you have any questions, comment below and I will answer.