#Requires -module DNSServer <# .Synopsis Removes Host and associated PTR records from Windows DNS server .DESCRIPTION This script removes a DNS Server Resource Record, this parses the IP to find and delete any associated PTR records from Windows DNS server. The script relies on the PowerShell DNSServer Module, which is available on Server 2008 and later. .EXAMPLE Remove-DNSRecord -RecordName WINSERVER01 -DNSServer dns1.contosco.com .EXAMPLE $DNSServer = "dns1.constoso.com" $list = "WINSERVER02A,WINSERVER03A,WINSERVER04A".split(",") $list | foreach {Remove-DNSRecord -RecordName $_ -DNSServer $DNSServer } .PARAMETER RecordName This is the host name you want to delete. Do not use the FQDN .PARAMETER DNSSERVER This is the Windows DNS Server. You may use the FQDN for this parameter. .NOTES Name: Remove-DNSRecord.ps1 Author: Alan Kaplan .LINK https://www.akaplan.com/blog #> Function Remove-DNSRecord { [CmdletBinding(SupportsShouldProcess=$true)] Param ( [Parameter(Mandatory=$true,Position=0)] # RecordName is the name of host or CNAMe to Delete [string]$RecordName, # DNSServer or domain name [Parameter(Mandatory=$true,Position=1)] [string]$DNSServer ) Begin {$NodeARecord=$null} Process { if ($pscmdlet.ShouldProcess($RecordName)){ $bTest = $False}Else{$bTest = $true} Write-Host "Getting Zones for $DNSServer" -ForegroundColor "Green" $Zones = @(Get-DnsServerZone -ComputerName $DNSServer) $NotLookup = $Zones | where {$_.ZoneName -notmatch 'arpa'} ForEach ($ZoneName in ($NotLookup).ZoneName) { Write-Host "Searching $ZoneName" -ForegroundColor "Green" $Zone | Foreach{ $NodeARecord = Get-DnsServerResourceRecord -ZoneName $ZoneName -ComputerName $DNSServer -Name $RecordName -ErrorAction SilentlyContinue if($NodeARecord){ Remove-DnsServerResourceRecord -ZoneName $ZoneName -ComputerName $DNSServer -InputObject $NodeARecord -Force -whatif:$bTest Write-Host ("A record deleted: "+$NodeARecord.HostName) Break } } } #See https://rcmtech.wordpress.com/2014/02/26/get-and-delete-dns-a-and-ptr-records-via-powershell/ if ($NodeARecord){ $IPAddress = $NodeARecord.RecordData.IPv4Address.IPAddressToString $IPAddressArray = $IPAddress.Split(".") $IPAddressFormatted = ($IPAddressArray[3]+"."+$IPAddressArray[2]) $ZonePrefix = ($IPAddressArray[1]+"."+$IPAddressArray[0]) $ReverseZoneName = "$ZonePrefix`.in-addr.arpa" $NodePTRRecord = Get-DnsServerResourceRecord -ZoneName $ReverseZoneName -ComputerName $DNSServer -Node $IPAddressFormatted -RRType Ptr -ErrorAction SilentlyContinue if($NodePTRRecord -eq $null){ Write-Host "No PTR record found" } else { Remove-DnsServerResourceRecord -ZoneName $ReverseZoneName -ComputerName $DNSServer -InputObject $NodePTRRecord -Force -WhatIf:$bTest Write-Host ("PTR Record Deleted: "+$IPAddressFormatted) } }ELSE{ Write-warning "No record for $RecordName found at $DNSServer" } }#End Process End{ Write "Done" } }