VMware Cloud Community
Viewaskew
Enthusiast
Enthusiast
Jump to solution

PowerCLI Script to list VMs by their hosts

Trying to find a simple script that will output to a CSV a list of VMs and which host they currently are under but no luck so far. Closest I’ve found is Alan Renouf’s one below that lists the number of VMs per host but not the actual names.

Get-VMHost | Select @{N=“Cluster“;E={Get-Cluster -VMHost $_}}, Name, @{N=“NumVM“;E={($_ |Get-VM).Count}} | Sort Cluster, Name | Export-Csv -NoTypeInformation c:\clu-host-numvm.csv

Im sure it’s something along the simple lines of get-vmhost | get-vm but need the host listed. Any ideas?

Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, -

There is a property, VMHost, of VM objects.  So, you can just use a Get-VM call piped to a Select-Object statement (and then to Export-Csv), like:

Get-VM | Select Name,VMHost | Export-Csv -Path c:\temp\VMsAndHostsInfo.csv -NoTypeInformation -UseCulture

That do it for you?

View solution in original post

Reply
0 Kudos
2 Replies
mattboren
Expert
Expert
Jump to solution

Hello, -

There is a property, VMHost, of VM objects.  So, you can just use a Get-VM call piped to a Select-Object statement (and then to Export-Csv), like:

Get-VM | Select Name,VMHost | Export-Csv -Path c:\temp\VMsAndHostsInfo.csv -NoTypeInformation -UseCulture

That do it for you?

Reply
0 Kudos
Viewaskew
Enthusiast
Enthusiast
Jump to solution

Thanks Matt, that works perfectly. I was just going to post that I found: Get-VM | Select-Object Name,VMHost which did the trick as well but thank you for the complete one liner.

Reply
0 Kudos