How to use PowerShell to manage DNS records (2022)

Managing Domain Name System (DNS) records is a repetitive task that often necessitates manual intervention. It can be tedious for an administrator to maintain records for a domain, and it’s easy to lose track of which records need updating and which ones don’t.

In this article, we will go over how PowerShell can be used to facilitate the process of managing DNS records at scale by automating repetitive tasks and making it easier for administrators to keep track of their DNS record changes.With the use of PowerShell, we can create scripts that will automate some of these manual tasks saving time as well as effort.

 This article will teach you 3 easy steps that allow you to manage DNS records using PowerShell.

1. Add a DNS record

2. Modify an existing DNS record

3. Delete an existing DNS record

Add a DNS record using PowerShell

The Add-DnsServerResourceRecordA cmdlet adds a host address (A) record to a DNS zone. An IPv4 address is specified by an A record. Below is the sytax for Add-DnsServerResourceRecordA.

				
					Add-DnsServerResourceRecordA
   [-AllowUpdateAny]
   [-CreatePtr]
   [-Name] <String>
   [-IPv4Address] <IPAddress[]>
   [-ComputerName <String>]
   [-TimeToLive <TimeSpan>]
   [-ZoneName] <String>
   [-AgeRecord]
   [-PassThru]
   [-ZoneScope <String>]
   [-VirtualizationInstance <String>]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
				
			

#Add a A record to the Zone PowerShellguru.com

Below example shows how to add a record “testrecord” to the zone “powershellguru.com” with IPv4 address “175.18.99.23” and time to live set for 8 days.

				
					Add-DnsServerResourceRecordA -Name "Testrecord" -ZoneName "powershellguru.com" -AllowUpdateAny -IPv4Address "175.18.99.23" -TimeToLive 08:00:00
				
			

Modify existing DNS record using PowerShell

Set-DnsServerResourceRecord modifies a resource record object in a Domain Name System (DNS) zone. You can use the OldInputObject parameter to specify an existing resource record object to change, and the NewInputObject parameter to specify a new resource record. This cmdlet does not have the ability to change the Name or Type of a DNS server resource record object. Below is the syntax of Set-DnsServerResourceRecord

				
					Set-DnsServerResourceRecord
   [-NewInputObject] <CimInstance>
   [-OldInputObject] <CimInstance>
   [-ComputerName <String>]
   [-ZoneName] <String>
   [-PassThru]
   [-ZoneScope <String>]
   [-VirtualizationInstance <String>]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
				
			

#Change the time span of a resource record

The time to live (TTL) value of the resource record named Host01 in the zone named contoso.com is changed to 2 hours in this example.The first command assigns to the variable $OldObj a resource record named Host01 in the zone contoso.com.The second command uses the.Clone() method to copy the variable $OldObj to a new variable $NewObj.The third command sets the $NewObj TTL time span to 2 hours.The fourth command sets the properties of $OldObj to the values specified in the previous command for $NewObj.

				
					$OldObj = Get-DnsServerResourceRecord -Name "Host01" -ZoneName "contoso.com" -RRType "A"
$NewObj = $OldObj.Clone()
$NewObj.TimeToLive = [System.TimeSpan]::FromHours(2)
Set-DnsServerResourceRecord -NewInputObject $NewObj -OldInputObject $OldObj -ZoneName "contoso.com" -PassThru

HostName                  RecordType Timestamp            TimeToLive      RecordData
--------                  ---------- ---------            ----------      ----------
Host01                       A          0                    02:00:00        2.2.2.2
				
			

Delete existing DNS record using PowerShell

The Remove-DnsServerResourceRecord cmdlet deletes resource record objects from a DNS zone.

You can either specify an object or the RRtype, Name, and RecordData of the resource record you want to remove with the Get-DnsServerResourceRecord cmdlet. If an RRtype or name is specified and there are multiple resource records, you can specify the RecordData to delete a specific record. If you do not specify RecordData, the cmdlet deletes all records for the specified zone that match RRtype and Name. Below is the syntax for the same.

				
					Remove-DnsServerResourceRecord
      [-ZoneName] <String>
      [-PassThru]
      [-ComputerName <String>]
      [-Force]
      [-ZoneScope <String>]
      [-VirtualizationInstance <String>]
      [-RRType] <String>
      [-RecordData <String[]>]
      [-Name] <String>
      [-CimSession <CimSession[]>]
      [-ThrottleLimit <Int32>]
      [-AsJob]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]
				
			

#Remove an A record

The below example will remove the A type record with IP address “10.17.1.41” and record name as “Host01”.

				
					Remove-DnsServerResourceRecord -ZoneName "contoso.com" -RRType "A" -Name "Host01" -RecordData "10.17.1.41"
				
			

Conclusion

Hope you have enjoyed reading How to use PowerShell to manage DNS records .PowerShell is a powerful, flexible tool that has a lot of uses and you might be surprised how much you can do using it, and managing DNS records is just one of them. There are many ways that you can use the PowerShell to manage your DNS records. I hope you have grasped some idea on managing DNS with PowerShell. 

If you have loved this article do check out the below as well.

https://powershellguru.com/dns-powershell-scripts/

 

Related Posts

Leave a Reply

Please disable your adblocker or whitelist this site!