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 thoughts on “List All DNS Records with Powershell

  1. 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

  2. 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.

  3. 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

    • 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

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

        • 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()

  4. 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]
    []

  5. @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”
    }

  6. 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

  7. 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.

  8. 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….

  9. 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”
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *