With the one-liner below, you’re able to create an overview of your VM’s, Clusters, ESX Hosts and Datastores.

Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `
@{N="ESX Host";E={Get-VMHost -VM $_}}, `
@{N="Datastore";E={Get-Datastore -VM $_}} 

The following output will be generated:


If you add an extra line with the export-csv cmdlet, you can export the output to a CSV file.

Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `
@{N="ESX Host";E={Get-VMHost -VM $_}}, `
@{N="Datastore";E={Get-Datastore -VM $_}} | `
Export-Csv -NoTypeInformation C:\Scripts\VM_CLuster_Host_Datastore.csv 

If you want, you can import the CSV file into Excel. Excel has some basic filtering options, so you’re able to filter on ESX Host, Cluster etc.



16 thoughts on “PowerCLI: One-Liner to get VMs, Clusters, ESX Hosts and Datastores

  1. Hi,

    Thanks a lot for this script. It will save me a lot of time to make a report of all my vms and their location.

    Thanks for sharing your job with us.
    God Bless You.

  2. This is great script. It helped me a lot.
    Additionally can you suggest how to get Total and free space details against each datastore.


  3. For example , storage team allocated lun, but that lun not added any cluser and esx, how to collect not added lun details for entire vc each cluser name wise ?

  4. That’s awesome but how do I get the datastores on separate lines? I want to use a vlookup table within Excel to match datastores against my storage arrays. Having the datastores concatenated (e.g., lun1, lun 2, lun 3) makes that impossible without manually separating them.

  5. I found the script run very slow, I have 40 ESX host, and about 1000 vms. I run this script for about 40 minutes, Could it run more quickly? If I donot care the Performance consume on the vCenter or the client I run the scripts. Thanks!

    1. I don’t know powershell, but it’s definitely spawning internal loops that are redundant for the “Get-Cluster -VM” filters. If you make them explicit it’s much faster:
      foreach ($cluster in Get-Cluster)
      $hosts = $cluster | Get-VMHost
      foreach ($each_host in $hosts)
      $vms = $each_host | Get-VM
      foreach ($vm in $vms)
      … etc

      You can probably condense the enumeration of each child object to occur within the “foreach” line, but I don’t know how.

  6. Thank you so much it’s very use full.
    I wan to extract the mismatch details between host and data store

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.