Managing DNS (Domain Name System) using PowerShell is a powerful way to automate and streamline your DNS administration tasks, especially in enterprise Windows Server environments. PowerShell provides full control over DNS zones, records, and settings, enabling sysadmins to execute complex operations with simple commands. In this blog, we will explore 15 practical PowerShell commands that every Windows DNS administrator should know.
1. List All DNS Zones
Get-DnsServerZone
Use this command to retrieve a list of all DNS zones hosted on your DNS server.
2. Create a New Forward Lookup Zone
Add-DnsServerPrimaryZone -Name "example.com" -ZoneFile "example.com.dns"
This command creates a new forward lookup zone called “example.com” with an associated zone file.
3. Create a New Reverse Lookup Zone
Add-DnsServerPrimaryZone -NetworkId "192.168.1.0/24" -ZoneFile "1.168.192.in-addr.arpa.dns" -ZoneType ReverseLookup
Use this to set up reverse name resolution for the 192.168.1.0 network.
4. Add a New A Record
Add-DnsServerResourceRecordA -Name "web" -ZoneName "example.com" -IPv4Address "192.168.1.100"
Creates an A record pointing “web.example.com” to the IP address 192.168.1.100.
5. Add a CNAME Record
Add-DnsServerResourceRecordCName -Name "alias" -HostNameAlias "web.example.com" -ZoneName "example.com"
Creates a canonical name record pointing “alias.example.com” to “web.example.com”.
6. Add an MX Record
Add-DnsServerResourceRecordMX -Name "@" -MailExchange "mail.example.com" -Preference 10 -ZoneName "example.com"
Adds a Mail Exchange record to route emails through “mail.example.com”.
7. Remove a DNS Record
Remove-DnsServerResourceRecord -ZoneName "example.com" -RRType "A" -Name "web" -Force
Deletes an A record named “web” from the “example.com” zone.
8. View All DNS Records in a Zone
Get-DnsServerResourceRecord -ZoneName "example.com"
Lists all records within the specified DNS zone.
9. Clear DNS Cache on Client
Clear-DnsClientCache
Flushes the DNS resolver cache on the local client.
10. Flush DNS Server Cache
Clear-DnsServerCache
Clears all cached DNS entries on the DNS server.
11. Set DNS Server Addresses on a NIC
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses ("8.8.8.8", "1.1.1.1")
Changes DNS servers on the “Ethernet” network adapter.
12. Get Current DNS Settings
Get-DnsClientServerAddress
Displays current DNS server settings for all adapters.
13. Export DNS Zone to File
Export-DnsServerZone -Name "example.com" -FileName "C:\Backup\example.com.dns"
Backs up the “example.com” zone to a file.
14. Import DNS Zone from File
Import-DnsServerZone -Name "example.com" -FileName "C:\Backup\example.com.dns"
Restores a zone from a previously backed-up file.
15. Monitor DNS Server Stats
Get-DnsServerStatistics
Displays various DNS server performance and operational statistics.
Conclusion
With PowerShell, DNS administration becomes faster, more reliable, and scriptable. Whether you’re adding records, configuring zones, or troubleshooting DNS issues, these commands will give you a solid foundation for managing your environment efficiently. Bookmark this list and incorporate it into your regular DNS maintenance routines.