How to get the Azure VM DNS name using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

We can find the DNS name of the VM from the Azure Portal as shown below from the Azure VM Overview tab.

This DNS setting is associated with the Public IP address. To retrieve the DNS settings we first need to retrieve the Public IP details. For this example, suppose we have the Azure VM TestMachine2k16 and we need to retrieve its DNS settings (Assuming you are connected to the proper Azure account and the subscription).

$vm = Get-AzVM -VMName TestMachine2k16
$pubip = Get-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName | where{$_.Id -match $vm.Name}

$pubip variable has the attached Public IP properties. You can retrieve the DNS name using its DNS property.

PS C:\> $pubip.DnsSettings


To get the FQDN, further expand that variable.

PS C:\> $pubip.DnsSettings.fqdn
Published on 28-Apr-2021 17:15:49