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

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 (“”,””)

Comments (8)

  1. tommymaynard says:

    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 = ""
    $NIC_SecondaryDNS = ""
    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 = ""
    $NIC_SecondaryDNS = ""
    Invoke-command -ComputerName $Server -cred $cred -ScriptBlock {Param($NIC1,$NIC2) Set-DnsClientServerAddress -InterfaceAlias corp -ServerAddresses("$NIC1,"$NIC2")} -ArgumentList $NIC_PrimaryDNS,$NIC_SecondaryDNS

  2. Nikolaj says:

    Neat trick!
    But could you please write some requirements. I know that Set-DNSClientServerAddress is not available on Windows 7. 🙂


  3. ris says:

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

  4. Rob says:

    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").

  5. Micah (The Powershell Ninja) says:


    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 = “”,””

    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.

Skip to main content