Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to set primary and secondary DNS server addresses with PowerShell

·221 words·2 mins· 100 views · 5 likes ·
DNS Enter-PSSession Get-DnsClientServerAddress Get-Help

In this post, I want to show how to configure your computer’s primary and secondary DNS servers using PowerShell. In this case, we will use the Set-DnsClientServerAddress cmdlet.

Set-DnsClientServerAddress
The workaround in a local computer: The Get-NetAdapter cmdlet obtains the basic properties of the network adapter. with this cmdlet, we get the index of the network adapter, among other data. Run the following PowerShell command as an administrator:

Get-NetAdapter `
    | Select-Object InterfaceAlias , InterfaceIndex

Get-NetAdapter
The Get-DnsClientServerAddress cmdlet gets one or more DNS server IP addresses associated with the interfaces on the computer.

Get-DnsClientServerAddress `
    -InterfaceIndex 9

Get-DnsClientServerAddress
To set the DNS server addresses using PowerShell, use Set-DnsClientServerAddress with the following syntax. Important: Run the following PowerShell command as Administrator:

Set-DnsClientServerAddress `
    -InterfaceIndex 9 `
    -ServerAddresses ("8.8.8.8","1.1.1.1")

The Set-DnsClientServerAddress sets the DNS server addresses on an interface using a specified index value.

DNS PowerShell
The workaround in a remote computer: The Enter-PSSession cmdlet starts an interactive session with a single remote computer.

Enter-PSSession `
    -ComputerName ComputerName `
    -Credential domainuser

The commands that you type run on the remote computer during the session.

Get-NetAdapter `
    | Select-Object InterfaceAlias , InterfaceIndex

Get-DnsClientServerAddress `
    -InterfaceIndex 9

set-DnsClientServerAddress `
    -InterfaceIndex 9 `
    -ServerAddresses ("10.10.10.100")

DNS PowerShell

Thanks for reading my post. I hope you find it useful.

If you want to know more about the Set-DnsClientServerAddress cmdlet, check out this link.