August 30th, 2014

PowerTip: Use PowerShell to Set Primary and Secondary DNS Server Addresses

Doctor Scripto
Scripter

Summary: Use Windows PowerShell to set the primary and secondary DNS server addresses for a client.

Hey, Scripting Guy! Question I recently changed the IP address scheme for an entire subnet. How can I use Windows PowerShell to set the
           primary and secondary DNS server addresses for the client workstations?

Hey, Scripting Guy! Answer Use the Set-DNSClientServerAddress cmdlet, and specify the primary and the secondary DNS servers as
           an array, for example:

Set-DNSClientServerAddress –interfaceIndex 12 –ServerAddresses (“10.0.0.1”,”10.0.0.2”)

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • Max

    What is the significance of the InterfaceIndex?  How can you know which one to chose? In my case, for example, the index interface I want is 7. 

    • Mike Thompson

      Hi I was just playing with this and I am not sure if your are still having an issue. If you use -interfacealias Ethernet*

      Set-DnsClientServerAddress -interfacealias Ethernet* -serveraddresses (“8.8.8.8,8.8.4.4”)

      This should change all wired connections DNS servers. I had to do this so I can set the secondary DNS for Static IP addresses in a SCCM task sequence

    • Pieterjan Rossaert

      DNS servers are interface-dependent. With the InterfaceIndex, you can define for which interface you change the DNS settings. Alternatively, you can define the interface with -InterfaceAlias. Both values are retrievable with the command Get-NetIPInterface.