How to set primary and secondary DNS server addresses with PowerShell
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. 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
The Get-DnsClientServerAddress cmdlet gets one or more DNS server IP addresses associated with the interfaces on the computer.
Get-DnsClientServerAddress `
-InterfaceIndex 9
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. 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")
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.