blog
Powershell – Change DNS IP Addresses remotely
Sometimes you need to correct DNS settings on multiple servers at once. Doing it manually through the network adapter UI works for one machine, but it does not scale well during migrations, DNS cutovers, or post-build standardization.
The basic idea is simple: find the right network adapter on the remote machine, set the DNS server addresses, and return the resulting configuration so you can verify the change immediately.
A safer remote function
This version uses CIM sessions and validates the adapter before applying the change.
function Set-RemoteDnsServerAddress {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
[string[]] $ComputerName,
[Parameter(Mandatory)]
[string] $InterfaceNameLike,
[Parameter(Mandatory)]
[string[]] $ServerAddresses
)
foreach ($Computer in $ComputerName) {
if (-not (Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {
Write-Warning "Skipping $Computer because it is offline."
continue
}
$CimSession = New-CimSession -ComputerName $Computer
try {
$Adapters = Get-NetAdapter -CimSession $CimSession | Where-Object Name -like $InterfaceNameLike
if (-not $Adapters) {
Write-Warning "No adapter matching '$InterfaceNameLike' found on $Computer."
continue
}
foreach ($Adapter in $Adapters) {
$Target = "$Computer / $($Adapter.Name)"
if ($PSCmdlet.ShouldProcess($Target, "Set DNS servers to $($ServerAddresses -join ', ')")) {
Set-DnsClientServerAddress -CimSession $CimSession -InterfaceIndex $Adapter.InterfaceIndex -ServerAddresses $ServerAddresses -Validate
Get-DnsClientServerAddress -CimSession $CimSession -InterfaceIndex $Adapter.InterfaceIndex -AddressFamily IPv4 |
Select-Object @{ Name = 'ComputerName'; Expression = { $Computer } }, InterfaceAlias, ServerAddresses
}
}
} finally {
if ($CimSession) {
$CimSession | Remove-CimSession
}
}
}
}
Example usage
Set-RemoteDnsServerAddress `
-ComputerName 'SRV-APP-01', 'SRV-APP-02' `
-InterfaceNameLike 'Ethernet*' `
-ServerAddresses '10.0.0.10', '10.0.0.11' `
-WhatIf
Remove -WhatIf when you are ready to apply the change for real.
Why this approach is better
- It works against multiple computers in one run.
- It checks whether the host is reachable before opening a session.
- It returns the final DNS configuration so you can verify the result.
- It supports
-WhatIf, which is useful during maintenance windows.
Important behavior to remember
Set-DnsClientServerAddress applies static DNS servers to the adapter. That means the configured values override DNS settings that would otherwise come from DHCP on that interface.
If you need to return an adapter to DHCP-provided DNS settings later, use:
Set-DnsClientServerAddress -InterfaceIndex 12 -ResetServerAddresses