Skip to main content

You want to have a backup of all the DNS records from your Windows DNS. The DNS Manager is very limited with such exports. The only way to get a good DNS records export is with PowerShell. In this article, you will learn how to export DNS records to CSV file with PowerShell.


        'MX' { $info.RecordData.MailExchange }
        'AAAA' { $info.RecordData.IPv6Address }
        'TXT' { $info.RecordData.DescriptiveText }
        default { $null }
    }
    $dnsRecords += [pscustomobject]@{
        Name       = $zoneName
        Hostname   = $info.Hostname
        Type       = $info.RecordType
        Data       = $recordData
        Timestamp  = $timestamp
        TimeToLive = $timetolive
    }
}
$dnsRecords | Export-Csv "C:\temp\SingleDNSZoneRecords.csv" -NoTypeInformation -Encoding utf8

The CSV file export will be in C:\temp. Here is what the CSV file looks like.

Single DNS zone records in CSV file

Going through all the zones and exporting them takes time. It’s handy if you only want to export a single DNS server zone but not if you’re going to export all DNS zones records.

Export all DNS zones records to CSV file with PowerShell

To export all DNS zones records, run the below PowerShell script. It will use the Get-DnsServerZone cmdlet to retrieve the DNS zones on a DNS server and the Get-DnsServerResourceRecord cmdlet to retrieve the DNS resource records from all zones.

The script will go through both the Windows DNS zones:

  • Forward Lookup Zones
  • Reverse Lookup Zones
$dnsRecords = @()
$zones = Get-DnsServerZone
foreach ($zone in $zones) {
    $zoneInfo = Get-DnsServerResourceRecord -ZoneName $zone.ZoneName
    foreach ($info in $zoneInfo) {
        $timestamp = if ($info.Timestamp) { $info.Timestamp } else { "static" }
        $timetolive = $info.TimeToLive.TotalSeconds
        $recordData = switch ($info.RecordType) {
            'A' { $info.RecordData.IPv4Address }
            'CNAME' { $info.RecordData.HostnameAlias }
            'NS' { $info.RecordData.NameServer }
            'SOA' { "[$($info.RecordData.SerialNumber)] $($info.RecordData.PrimaryServer), $($info.RecordData.ResponsiblePerson)" }
            'SRV' { $info.RecordData.DomainName }
            'PTR' { $info.RecordData.PtrDomainName }
            'MX' { $info.RecordData.MailExchange }
            'AAAA' { $info.RecordData.IPv6Address }
            'TXT' { $info.RecordData.DescriptiveText }
            default { $null }
        }
        $dnsRecords += [pscustomobject]@{
            Name       = $zone.ZoneName
            Hostname   = $info.Hostname
            Type       = $info.RecordType
            Data       = $recordData
            Timestamp  = $timestamp
            TimeToLive = $timetolive
        }
    }
}
$dnsRecords | Export-Csv "C:\temp\AllDNSZonesRecords.csv" -NoTypeInformation -Encoding utf8

It will export all the DNS records to a CSV file in C:\temp. This is what the CSV file looks like.

All DNS zones records in CSV file

Leave a Reply