Thursday, July 9, 2015

powershell to list old dns records and then delete them

$records = Get-DnsServerResourceRecord -ZoneName
$records = $records | ? Timestamp -ne $null
$records = $records | ? Timestamp -lt 3/01/2015
Write-Output $records

copy the output to a file then regex below in notepad++ to just get the hosts


The use the file as an import for the following powershell

$DNSServer = ""
$DNSZone = ""
$InputFile = "hosts.txt"
import-module DnsServer
$recordnames = Get-Content $InputFile

# Now we loop through the file to delete the records
ForEach ($recordname in $recordnames) {
Remove-DnsServerResourceRecord -Name $recordname -RRType A -ZoneName $DNSZone -ComputerName $DNSServer -Force