Adding name servers to multiple DNS zones with PowerShell

I ran into a little problem today where I needed to add multiple DNS servers as name servers to multiple DNS zones all in one go. So this is essentially adding NS resource records to a zone, but doing it for multiple zones all at once. Yes I could have done them manually, but that’s boring and time consuming. So, here’s a quick one-liner that does the trick, obviously substitute in your DNS server and name server FQDNs in the correct places. If it fails for any reason it will continue on, but report the zone it failed on.

Get-DnsServerZone -ComputerName | where {$_.zonetype -eq "primary" -or $_.zonetype -eq "secondary"} | ForEach-Object {try {Add-DnsServerResourceRecord -ZoneName $_.zonename -ns -ComputerName -name $_.zonename -NameServer -ea:stop} catch {"$_"}}

Hope someone finds that useful.