Get the IP addresses of Hyper-V VMs, part 2

A variation on my previous post.

This one-liner uses an expression in a select to expose the IP addresses. The expression is necessary so we can drill down into the NetworkAdapters collection to get to the IPAddresses property.

This will list all IP addresses for each VM:

Get-VM | select Name, State, @{Name="IP";Expression={$_.NetworkAdapters.IPAddresses}}

This next variation displays only the first IP address for each VM. Note the “[0]”. It addresses the first element of “IPAddresses” array.

Get-VM | select Name, State, @{Name="IP";Expression={$_.NetworkAdapters.IPAddresses[0]}}