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

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

  • Comments 7
  • Likes

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”)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Neat trick!
    But could you please write some requirements. I know that Set-DNSClientServerAddress is not available on Windows 7. :-)

    Thanks!

  • why cant I do this ?
    #$NIC_PrimaryDNS = "2.2.2.2"
    #$NIC_SecondaryDNS = "3.3.3.3"
    Invoke-command -ComputerName $Server -cred $cred -ScriptBlock {Set-DnsClientServerAddress -InterfaceAlias corp -ServerAddresses("$NIC_PrimaryDNS","$NIC_SecondaryDNS")}

  • thanks

  • @ris
    You can't do that because you're creating local variables and expecting the remote computer to know something about them. If you're using PS 3.0 or greater (on your local computer), then use the Using scope modifier.

    $NIC_PrimaryDNS = "2.2.2.2"
    $NIC_SecondaryDNS = "3.3.3.3"
    Invoke-command -ComputerName $Server -cred $cred -ScriptBlock {Set-DnsClientServerAddress -InterfaceAlias corp -ServerAddresses("$Using:NIC_PrimaryDNS","$Using:NIC_SecondaryDNS")}

    If you're using 2.0, then you'll have to use parameters to pass in your local variables.

    $NIC_PrimaryDNS = "2.2.2.2"
    $NIC_SecondaryDNS = "3.3.3.3"
    Invoke-command -ComputerName $Server -cred $cred -ScriptBlock {Param($NIC1,$NIC2) Set-DnsClientServerAddress -InterfaceAlias corp -ServerAddresses("$NIC1,"$NIC2")} -ArgumentList $NIC_PrimaryDNS,$NIC_SecondaryDNS

  • Does it work with PowerShell 4.0 in Windows 7? I got an error ("The term 'Set-DnsClientServerAddress' is not recognized as the name of a cmdlet, function, script file, or operable program").

  • @Rob

    In this case, version is irrelevant. Windows 8/Server 2012 and higher come with additional modules of advanced functions to help manage the OS. They are not integrated Powershell Cmdlets.

    You can use WMI to so the same thing. If you know your Interface Index, you could use this:
    $NetworkAdapterConfig = Get-WMIObject -Class Win32_NetworkAdapterConfiguration -Filter {Index = 12}
    $NetworkAdapterConfig.DNSServerSearchOrder = “10.0.0.1”,”10.0.0.2”

    This is Version 2 safe and should work on any Windows OS (XP or above). Please note that this uses WMI and is not networking friendly. I would sugest running this inside of a PSSession of some kind if you are setting this remotely.