# Powershell – Change DNS IP Addresses remotely

Use PowerShell to change DNS server addresses on remote Windows machines with a safer pattern that validates adapters and returns the final configuration.

blog

09 Feb 2016 pl

![Powershell – Change DNS IP Addresses remotely](/wp-content/uploads/2015/07/PowerShell-Automating-Administration.jpg)

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.

```powershell
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

```powershell
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:

```powershell
Set-DnsClientServerAddress -InterfaceIndex 12 -ResetServerAddresses
```

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [PowerShell - zdalna zmiana ustawień adresów DNS](/blog/powershell-zdalna-zmiana-ustawien-adresow-dns/)
- [PowerBGInfo 2.x: support-ready Windows backgrounds from PowerShell](/blog/powerbginfo-support-ready-windows-backgrounds/)
- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)

<!-- PowerForge markdown artifact sha256:941770C929866E18FDD11DF1AB8070767B9D851B8065BC422995C619CD3D2BCA -->
