Replace NSLookup with Resolve-DnsName

Replace NSLookup with Resolve-DnsName

So we have all been there where we need to confirm that the DNS record exists, or you have a script which needs the IP address rather than the DNS address. Traditionally we would use NSLookup to validate all of this.
Recently I have needed to complete a solution based in PowerShell that I needed to create a DNS record and then validate that it exists, and that the IP address has been set correctly.
Rather than writing heaps of extra code to handle the result from NSLookup in PowerShell, or to instantiate the DNS .Net object, I spent the time to look into the PowerShell options.
This is where I stumbled upon Resolve-DnsName which is fantastic, it allows you to not only complete a generic lookup of the DNS address, but you can use the -type parameter for example -type A will bring back only the A name records for that DNS address for example:

If we were to then run the same query with AAAA as the type (IPv6 Address) we get the following:

Where the Commandlet comes into its own is that you can save it into an object and then use the result to complete your tasks which require an IP address, for example like this:

Obviously this is just scratching the surface of what you can do with the commandlet, but hopefully a starting point for people to think about using more PowerShell.
Good Luck

Leave a Reply