How To Add Extra DNS Server Using PowerShell (2022)

Add extra DNS server using PowerShell, ever wonder if you have to add a new or a third DNS server in 100 VMs but you don’t want to do it, well I have the best and easy solution on how you can do it with PowerShell. We will use the array concept here to add a new DNS server to the NICs of multiple Windows servers. This will be a small and interesting script so do it if you have the urgency.

Set-DnsClientServerAddress: Usage

You got the idea correctly that set-dnsclientserveraddress will be the key cmdlet for our PowerShell script so let’s check the syntax for it. It is used to set the DNS server addresses associated with an interface’s TCP/IP properties. The syntax below is self-explainatory.

				
					Set-DnsClientServerAddress
   [-InterfaceAlias] <String[]>
   [-ServerAddresses <String[]>]
   [-Validate]
   [-ResetServerAddresses]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
				
			

Get-DnsClientServerAddress: Usage

Another cmdlet we will use in our script is Get-DnsClientServerAddress which will be used to get the DNS server IP addresses from an interface’s TCP/IP properties. Below is the syntax.

				
					Get-DnsClientServerAddress
   [-InterfaceIndex <UInt32[]>]
   [[-InterfaceAlias] <String[]>]
   [-AddressFamily <AddressFamily[]>]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [<CommonParameters>]
				
			

Add extra DNS server using PowerShell

So let’s understand step by step how to implement the script.

#Step 1

The use of the vintage Foreach loop.

				
					$computer = "WIN-S6DSAK4IUQC","WIN-P7PHNSHBNLB"
foreach ($computers in $computer){

				
			

#Step 2

We are using invoke-command to create a session on remote server and run the given commands remotely.

				
					$COMPUTERS
Invoke-Command -ComputerName $computers -ScriptBlock {
				
			

#Step 3

$add will provide us the value of the already present DNS server IP on the remote servers. Also, note it will only provide the IPv4 IPs in the NIC, not IPv6 as we have only mentioned IPv4 in the script.

				
					$add=Get-DnsClientServerAddress -InterfaceAlias Ethernet0 -AddressFamily IPv4 |select -ExpandProperty serveraddresses
				
			

#Step 4

The magic begins from here the IP address generated above will be assigned to the arrays $add[0] and $add[1] and we will add the required DNS server IP which needs to be added to remote servers.

Example, if the #Step 3 generates 10.0.0.1 and 10.0.0.2 as the result then $add[0] will be equal to 10.0.0.1 and $add[1] will be 10.0.0.2.

We are using $add[0] and $add[1] because we don’t want to disturb the already assigned DNS server IPs, hence if you only need to add third DNS IP then it will be best not to touch them.

				
					Set-DnsClientServerAddress -InterfaceIndex 6 –ServerAddresses ($add[0],$add[1],"10.56.12.12")
				
			

#Step 5

We will again run Get-DnsClientServerAddress so that we will get to know if the new DNS server IP is added to the NIC of the remote servers.

				
					Get-DnsClientServerAddress -InterfaceAlias Ethernet0 -AddressFamily ipv4 |select -ExpandProperty serveraddresses }}

				
			

Conclusion

I hope you have liked the post on How to Add Extra DNS server using PowerShell and will implement this whenever it is required. Things you should always remember is to try with one server so that it will be easy for you to make changes. Let me know if you want a blog post on some other script that might amaze you.

We are working continuously to provide you with the better and the best scripts daily. We will publish weekly hence don’t forget to subscribe to our newsletter.

Related Posts

Leave a Reply

Please disable your adblocker or whitelist this site!