Reading Azure VM name, IP address, and hostname with PowerShell

In this post, you will learn how to create a report to retrieve the virtual machine (VM) name, the IP address, and the guest OS hostname of every Azure VM using PowerShell. I'll explain how to do this for Azure classic and Azure Resource Manager (ARM).

Let's start with the classic model. Configuring VM networks in Azure is very similar to on-premises solutions such as Hyper-V. Each VM is assigned to at least one virtual network interface and can't serve as a standalone instance. Because a VM network interface can't even exist without VMs, my task is pretty easy. I just have to get all the cloud service names with their VM names and select the required properties.

Let's look at the properties of a classic Azure VM to decide which properties we have to add to the report:

Properties of a classic Azure VM

Properties of a classic Azure VM

The following command retrieves the properties I need for my report:

Name, HostName, and IpAddress properties of a VM

Name, HostName, and IpAddress properties of a VM

Then I can just add Export-CSV to that and export the output to an Excel spreadsheet:

In ARM, this task is a bit more complicated. Because the VM and the network interface are two different instances, I need to find a way to connect one to the other for my report.

First, let's take a look at the ARM VM properties:

ARM VM properties

ARM VM properties

As you can probably see from the screenshot, it's not that obvious to find the properties we need for the report. Even though the VM name and resource name are available, the IP address and guest OS name are missing. Fortunately, the guest OS name is just hidden in the OSProfile property. To access the guest OS name, I can run this command:

Extracting the guest OS hostname from the Azure ARM VM

Extracting the guest OS hostname from the Azure ARM VM

I just save the VM data to the variable and then read the information from the OSProFile.ComputerName field, which corresponds to the guest hostname.

Now I need to get an IP address, and as it turns out, it is not that simple. First, there is no IP address field. There is, however, the NetworkInterfaceIDs property. Even though it doesn't give me an IP address, it gives me network interface name. So I can try to extract the information from that, using the Get-Azurermnetworkinterface cmdlet:

ARM network interface properties

ARM network interface properties

As you can see, there's a lot of information. However, for now I'm going to use only the IP address and make a note for myself that the VirtualMachine field has an Id property and that the ID corresponds to VmId, which I previously received using Get-Azurerm cmdlet. Thus, I'll use this property to match the VM and network interface in my report:

Because I need to get the information from two sources instead of one, I'm storing all of my ARM VMs in the $vms variable and all of my interfaces in $nic. You can also notice that I'm filtering the network interfaces, getting only those where VirtualMachine property is not empty.

The reason is that I am only interested in interfaces assigned to the VMs. I'm going through the network interfaces one by one, checking if the Id property of the current VM matches the $ If it does, I find the VM that owns this particular network interface, so I put the information about this VM into the $info variable I prepared in advance. At the end of each iteration, I'm adding the data from the $info variable to the custom PowerShell object stored in the $report variable I created at the beginning.

And this is what my report for the ARM VM looks like:

Azure ARM VM report

Azure ARM VM report

Of course, you can export this report to a CSV file using the Export-Csv cmdlet.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

  1. Roman Gelman 3 years ago

    Great idea, after some attempts, I have made this 1-liner:

    Get-AzureRmNetworkInterface |select -Property `
    @{N='VM';E={[regex]::Match($_.VirtualMachine.Id, 'virtualMachines/(.+)$').Groups[1].Value}},
    @{N='IP'; E={'[' + ($_.IpConfigurations.PrivateIpAddress -join '] [') + ']'}},
    @{N='Subnet'; E={$script:subnet = if ($_.IpConfigurations.Subnet.Id.GetType().Name -eq 'string') `
    {$_.IpConfigurations.Subnet.Id} else {$_.IpConfigurations.Subnet.Id[0]} `
    [regex]::Match($script:subnet, 'subnets/(.+)$').Groups[1].Value}} |sort Subnet, VM |ft -au

    This approach supports multiple IPs

  2. David 3 years ago


    I have a quick question. Hopefully, you can assist me or at least point me to the right solution:

    I've an urgent project to complete , where I have to look/search for specific value/number ( =>100) in column "P" in massive multiple excel files that I can point to (data sets). After that, I need to print out/save a report with folder name where that particular value was found. I was wondering if Powergrep can do this type of search. I'm very short on time.I've tried to play with Powergrep around, but couldn't find an option where I can look at specific column in Excel files.Here is an example of folder structure and what I need to look for and what is my output should be:

    Looking at column "P" in excel ( column called "Temperature") for value that is =>100

    C:\Folder\ Folder1 Folder2

    Folder1 Excel1 Excel2 Excel3

    Folder2 Excel10 Excel200

    Output: The value was found @: Folder2

    Any help will be much appreciated!

    Thanks! David


  3. Nazim Rajani 2 years ago

    Thanks for this. Just one more thing I would like to add here was that with $info.OStype = $vm.StorageProfile.osDisk.osType you can also get the OSType details.

    I edited your script like below:

    $report = @()
    $vms = get-azurermvm
    $nics = get-azurermnetworkinterface | ?{ $_.VirtualMachine -NE $null}

    foreach($nic in $nics)
    $info = "" | Select VmName, ResourceGroupName, IpAddress, OSType
    $vm = $vms | ? -Property Id -eq $
    $info.VMName = $vm.Name
    $info.ResourceGroupName = $vm.ResourceGroupName
    $info.IpAddress = $nic.IpConfigurations.PrivateIpAddress
    $info.OStype = $vm.StorageProfile.osDisk.osType


  4. ankita 1 year ago

    can you help to create a powershell or .net code for extracting all the resources and sub-resources associated if we pass a parent resource name.


    ex: if we say a vm name, then all the NICs, NSG, disks, extensions, etc that is being used in the resource should be listed

    I am trying to create a command but getting error.


Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account