Clean up records on Windows Server DNS using PowerShell

on

Create a script and execute it on your server.

Set up a zone, the Windows Server running the DNS server, a date and the record type.

Import-mode DNSServer 
$zone="ZONE.TLD" 
$DNSServer="DNSHOSTNAME" 
$beforedate="12/20/2015" 
$recordtype="A" 
$records=Get-DnsServerResourceRecord -ZoneName "$zone" -ComputerName $DNSServer | 
Where-Object {$_.RecordType -eq "$recordtype" -and $_.TimeStamp -lt $beforedate} 
Foreach ($record in $records) 
{ 
# Remove the DNS record by filtering 
Try 
{ 
$hostadi=$record.HostName 
Remove-DnsServerResourceRecord -ZoneName $zone -ComputerName $DNSServer -Force -RRType "$recordtype" -Name $record.HostName 
Write-Host ("[{0}] deleted record name is : $hostadi" -f (Get-Date)) 
("[{0}] delete record name: $hostadi" -f (Get-Date)) | out-file "c:\DNSRecordRemoveLog.txt" -Append 
} 
Catch 
{ 
Write-Host ("[{0}] Cannot delete the record: $hostadi" -f (Get-Date)) 
("[{0}] Cannot delete the record name: $hostadi" -f (Get-Date)) | out-file "c:\DNSRecordRemoveLog.txt" -Append 
} 
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *