Clean up records on Windows Server DNS using PowerShell


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 
$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 
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 
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 *