How to Backup all Microsoft DNS Zones

# Get the name of the server with the env variable
$DNSServer=get-content env:computername

# Define folder where to store DNS backup
$BackupFolder="C:\Windows\System32\DNS\Backup"

# Define file name where to store DNS settings
$DNSFile=Join-Path $BackupFolder "input.csv"

# Check if the folder already exists. If exists, delete all content
if (-not(test-path $BackupFolder)) {
new-item $BackupFolder -Type Directory | Out-Null
} else {
Remove-Item $BackupFolder"\*" -recurse
}

# Get DNS settings using WMI Object
$List = get-WMIObject -ComputerName $DNSServer -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone

# Export information into input.csv file
$List | Select Name,ZoneType,AllowUpdate,@{Name="MasterServers";Expression={$_.MasterServers}},DsIntegrated | Export-csv $DNSFile -NoTypeInformation

# Call Dnscmd.exe to export DNS zones
$list | foreach {
$path="backup\"+$_.name
$cmd="dnscmd {0} /ZoneExport {1} {2}" -f $DNSServer,$_.Name,$path
Invoke-Expression $cmd
}

#End of script

Comments