VMware Cloud Community
Jump to solution

List VMs in datastore that is not in inventory

Hi Guys

PowerCLI rookie here, so sorry for stupid questions.

I’m trying to clean up a bunch of singehost with local datastores. So I need a script that can output a list of virtual machine files on a datastore that is not being used by VMs in the inventory. One of the problem is that the files on the datastore, is not all ways named exactly the same as the VM’s in the inventory.

Been looking at this, but I think the term “orphaned” is ‘sent what I think it is:


Also there is this one, which I think I should be able to modify to do what I want:


Any hints or clues to push me in the right direction would be appreciated.

62 Replies
Jump to solution

Hi LucD

so i have to run these all script into CLI ?

Get-FilesIdentifiedAsAssociatedToAllVMs, Get-FileInDatastore,get-FileInDatastoreWithWorkflow & get-ProbablyOrphanedFile

Means copy each of the script and paste into CLI ? then only can work?

Also for SSD or non-ssd, i just have to enter the datastore name rather then SSD/Non-SSD.?

0 Kudos
Jump to solution

You can paste the code on the prompt, but a better way would be to place all the functions n a .ps1 file.

And then dot-source this file. That way the functions are known to your PowerShell session.

Now you can make the call of the function.

Yes, the Where-clause you are using uses the datastorename.

Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Jump to solution

Luc Dekens has the right answer as always.

You need first the others functions. (I like the copy paste approach, even if it is not the best)

Keep in mind that the function get-ProbablyOrphanedFile  is actually quite complex with a mix of API call, PowerShell workflow, call of others function and there are no errors handling.

Moreover, it was developed a long time ago, and I have not tested it with new version. It should still work if the API have not been modified but this is not guaranteed.

It is suitable for experienced PowerCLI user that will have the skills to adapt and troubleshoot it as needed.

For your scenario, I will recommend a step by step troubleshooting.

Step 1: Get the correct list of datastores

Get-datastore | where {$_.something -eq “something”}

I do not think that there is a “Drive Type” property for a PowerCLI datastore object, so it is not possible to use something like $_.DriveType -eq "SSD".

Online Documentation - Cmdlet Reference - VMware {code}

However, I think the information that you are looking for is the “ssd” property of the API object “Host.VmfsVolume”


It is possible to “jump” from a powershell Datastore to an API datastore via “.extensiondata” property.

For more details about the core concepts of PowerCLI:


(This guide is not up to date but the majority of concepts still apply today)

Step 2: Try the function “Get-FileInDatastore” alone with only one datastore and your pattern

Get-datastore “Datastorename” | Get-FileInDatastore –matchPattern “YourPattern”

The goal is to check if the pattern is correct. (I am not sure if the pattern “*.vmx”| ogv" works.)

Actually, the format expected for this “pattern” is not well documented in the API.


Step 3: Try the function “get-FileInDatastoreWithWorkflow” alone

Step 4: Try the function “Get-FilesIdentifiedAsAssociatedToAllVMs” alone

Step 5: Try the function "get-ProbablyOrphanedFile"

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
0 Kudos