List All DNS Records with Powershell

UPDATED 6/16/2016 Thanks for the comments!

Here’s a nice quick script to list all DNS records in each zone on the DNS server(includes sub-zones):

From the DNS Server

$Zones = @(Get-DnsServerZone)
ForEach ($Zone in $Zones) {
	Write-Host "`n$($Zone.ZoneName)" -ForegroundColor "Green"
	$Zone | Get-DnsServerResourceRecord
}

From a Remote DNS Server

$DNSServer = "servernameOrIp"
$Zones = @(Get-DnsServerZone -ComputerName $DNSServer)
ForEach ($Zone in $Zones) {
	Write-Host "`n$($Zone.ZoneName)" -ForegroundColor "Green"
	$Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer
}

From a Remote DNS Server (Output to Tab Delimited File)

$DNSServer = "servernameOrIp"
$Zones = @(Get-DnsServerZone -ComputerName $DNSServer)
ForEach ($Zone in $Zones) {
	Write-Host "`n$($Zone.ZoneName)" -ForegroundColor "Green"
	$Results = $Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer
	echo $Results > "$($Zone.ZoneName).txt"
}

 

18 comments

Skip to comment form

    • Steve on March 24, 2016 at 9:42 am

    Great short script. Thank you for posting it. May I recommend that you name your output file with the extension .CSV since you are exporting CSV data and add the “-ComputerName” parameter to both your Get-DnsServerZone and Get-DnsServerResourceRecord so that it can be run from anywhere not just from the DNS server itself.

    Anyway, thank you for your post

    1. Thanks for the input, did a small update based on this

    • Riku Partanen on June 16, 2016 at 1:00 am

    It does not return any IP-addresses to CSV. If you run it without pipeing to anywhere, you can see IP-addressess though. Might be related the properities having sub-properties.

    1. Just did a quick mod that will output the data correctly

    • Michel Rock on July 8, 2016 at 11:15 am

    Hello great script, but i have some long entry that when exported they appear like this:

    x._domainkey.cyberoffres TXT 0 00:05:00 k=rsa; p=DEW22WKEJRNLqXXX222DQ98ASDKKGNADCBi…

    Is there a way to export the complete character??

    Best regards

      • Michel Rock on July 8, 2016 at 12:58 pm

      For futur generation 🙂 I got my solution here’s what i added

      $DNSServer = “servernameOrIp”
      $Zones = @(Get-DnsServerZone -ComputerName $DNSServer)
      ForEach ($Zone in $Zones) {
      Write-Host “`n$($Zone.ZoneName)” -ForegroundColor “Green”
      $Results = $Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer | Format-Table -AutoSize -Wrap
      echo $Results > “$($Zone.Zo

        • Kesavan P on May 18, 2021 at 12:37 am

        i have multiple servers but i need to take DNS server details for .xlsx .how can i get it. using power shell script

        1. I would suggest outputting to CSV then you can easily convert to XLSX by opening/saving. If you wanted to automate this, you can use COM objects to do this, but don’t really recommend it (Neither does Microsoft) bc office is designed to work on an interactive desktop not scripted. That being said, here’s a simple example to convert (Note: You must have Excel installed):

          $excel = New-Object -ComObject Excel.Application
          $excel.Visible = $true
          $excel.Workbooks.Open(“data.csv”).SaveAs(“data.xlsx”,51)
          $excel.Quit()

    • ShadowFly on December 7, 2017 at 12:43 pm

    Is there any way to get this to sort by timestamp? Or at least put the csv so the timestamp is in a separate field? I didn’t see any Parameters to do this:

    Get-DnsServerResourceRecord
    [[-Name] ]
    [-ComputerName ]
    [-ZoneName]
    [-Node]
    [-ZoneScope ]
    [-VirtualizationInstance ]
    [-RRType ]
    [-CimSession ]
    [-ThrottleLimit ]
    [-AsJob]
    []

    • Unggoy on February 28, 2018 at 10:02 am

    Try this:

    GetDnsServerResourceRecord – | Sort TimeStamp

    or

    GetDnsServerResourceRecord – | Export-Csv

    • Jerome on July 9, 2018 at 3:15 am

    Is there anyway to import from a remote server and save the output to a CSV file?

    • evasive2010 on August 28, 2018 at 3:47 am

    @ShadowFly: you need to sort the output before writing it down, there’s a separate Sort-Object cmdlet for this, I have added your requirement in the code:
    $DNSServer = “servernameOrIp”
    $Zones = @(Get-DnsServerZone -ComputerName $DNSServer)
    ForEach ($Zone in $Zones) {
    Write-Host “`n$($Zone.ZoneName)” -ForegroundColor “Green”
    $Results = $Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer | Sort-Object Timestamp -Descending | Format-Table -AutoSize -Wrap
    echo $Results > “$($Zone.ZoneName).txt”
    }

      • Angel on October 26, 2018 at 8:21 am

      tks a lot

    • Lawson23 on March 18, 2019 at 4:18 pm

    May I recommend:
    # https://github.com/lawson2305/Powershell/blob/master/DNSZoneRecord.ps1

    $DNSServer = “SERVER”
    $Zones = @(Get-DnsServerZone -computername $DNSServer)
    #
    # Get all zone information for a Domain Zone
    #

    $DNS = ForEach ($Zone in $Zones) {
    Get-DnsServerResourceRecord -Zonename $zone.ZoneName -ComputerName $DNSServer | Select-Object hostname, recordType, @{n=’ZoneName’;Expression={$zone.ZoneName}},@{n=’Data’;e={
    $rr = $_
    switch ($rr.RecordType) {
    A’ {$rr.RecordData.IPv4Address}
    CNAME’ {$rr.RecordData.HostnameAlias}
    NS’ {$rr.RecordData.NameServer}
    SOA’ {$rr.RecordData.PrimaryServer}
    SRV’ {$rr.RecordData.DomainName}
    PTR’ {$rr.RecordData.PtrDomainName}
    MX’ {$rr.RecordData.MailExchange}
    AAAA’ {$rr.RecordData.IPv6Address}
    TXT’ {$rr.RecordData.DescriptiveText}
    }
    }}}

    $DNS | Export-Csv -NoTypeInformation -Path dns.csv

    • prasad on July 30, 2019 at 12:06 pm

    Hi Can you please tell me
    How to get the DNS entries for about 20 hosts , listed in a zone file, so we can run it against different DNS servers and Then compare results.

    • Rob Ingenthron on July 30, 2019 at 5:45 pm

    I’m late to the party: this only works from a DNS server (Windows running the Windows DNS service), so it won’t run on a workstation, even with RSAT. dnscmd works from a workstation or a server. A sad limitation for PowerShell as we progress into the 21st Century….

    • sabofx on November 14, 2019 at 12:02 pm

    Should you only be interested in simple, ip records try this:

    #
    # Description:
    # Outputs all DNS records where a hostname resolves to an IP address
    # Output is grouped by DNSzone and written to both console and csv file
    # filename is determined from the zonename
    #
    $strOutputPath = “c:\temp”
    md $strOutputPath -force
    $DNSServer = read-host -Prompt “Enter DNS Server name or IP”
    $Zones = @(Get-DnsServerZone -ComputerName $DNSServer)
    ForEach ($Zone in $Zones) {
    Write-Host “`n$($Zone.ZoneName)” -ForegroundColor “Green” | ft -AutoSize
    $Results = $Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer
    $zoneRecords = @()
    foreach ($record in $Results) {
    $strHostname = $record.HostName
    $strIp = $record.Recorddata.IPv4Address.IPAddressToString
    if ($strIp) {
    $zoneRecord = $(“$strHostname,$strIp”)
    Write-Host $zoneRecord
    $zoneRecords += $zoneRecord
    }
    }
    if ($zoneRecords) {
    $zoneRecords | Sort-Object | Out-File “$strOutputPath\$($Zone.ZoneName).csv”
    }
    }

    • DURYODHAN DORA on March 4, 2021 at 12:49 am

    i need a script which will provide remote servers dns ip details.

Comments have been disabled.