PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
No Result
View All Result
Home Powershell Script blogs

How To Add Extra DNS Server Using PowerShell (2022)

themeanmachine19@gmail.com by [email protected]
July 7, 2022
in Powershell Script blogs
0
Add extra dns server using powershell
Share on FacebookShare on Twitter

You might also like

Instant guide to convert ps2 to exe

Instant Guide To Convert PS1 To EXE (2022)

May 21, 2022
PowerShell tips and tricks

5 Useful PowerShell Tips and Tricks

May 15, 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.

Add extra DNS server using PowerShell

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.

close

DON’T MISS A POST

Keep up to date with PowershellGuru

Powershell Blogs

PowershellGuru provides the best PowerShell scripts available that can be used and download freely. Do Check our blogs to get updated regularly.

Check your inbox or spam folder to confirm your subscription.

themeanmachine19@gmail.com

[email protected]

Related Stories

Instant guide to convert ps2 to exe

Instant Guide To Convert PS1 To EXE (2022)

by [email protected]
May 21, 2022
0

Instant guide to convert ps1 to exe. How to convert ps1 to exe. PS2exe. Convert ps1 to exe using ps2exe.

PowerShell tips and tricks

5 Useful PowerShell Tips and Tricks

by [email protected]
May 15, 2022
0

Useful Powershell tips and tricks. PowerShell tips and tricks. Know Powershell tips and tricks. get-help. get-alias.

tips to manage hyper-v using powershell

5 Tips To Manage Hyper-V Using PowerShell

by [email protected]
May 1, 2022
0

Tips to manage hyper-v using PowerShell. How to manage Hyper-V using PowerShell. Powershell to manage Hyper-V. Hyper-v and PowerShell.

Mistakes To Avoid While Writing A Powershell Script

5 Big Mistakes To Avoid While Writing A Powershell Script

by [email protected]
April 30, 2022
0

Mistakes to avoid while writing a powershell script. PowerShell mistakes. Avoid these mistakes in a powershell script. PowershellGuru.

Please login to join discussion

Recommended

Add users to multiple groups using PowerShell

Add users to multiple groups using PowerShell from CSV (2021)

November 15, 2021

Get-LocalGroupMember: Find Local admin using PowerShell (2021)

September 18, 2021

About

Dhrub Bharali

PowerShell Enthusiast

Dhrub is hardcore Powershell enthusiast, he has wriiten more than 100 powershell scripts and he is the sole owner of PowerShellGuru.

Follow Us

Popular Story

  • Installing software remotely using powershell

    Easy way to install software remotely using PowerShell (2021)

    1059 shares
    Share 424 Tweet 265
  • How to find NTP Server using PowerShell?

    938 shares
    Share 375 Tweet 235
  • Get-LocalGroupMember: Find Local admin using PowerShell (2021)

    889 shares
    Share 356 Tweet 222
  • How to Log off Users Remotely with PowerShell (2022)

    816 shares
    Share 326 Tweet 204
  • Powershell filter: Using where-object and select-object (2021)

    799 shares
    Share 320 Tweet 200
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

No Result
View All Result
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.