Yes, but I see that I left something out in my question;
Regarding the folder, I want to output all VMs from the "top" folder.
As in
-Cuscomer1
-test
-dev
-Customer2
-test
-dev
-Customer3
-test
-dev
I want the "folder" column in the output csv to list if the VMs is located in the folder Customer1, 2 or 3 (not test or dev)
If I use this;
I only get the direct folder the VM is located under (test or dev)
$VMs = Get-VM
$Output = foreach ($VM in $VMs){
Get-VM $VM | select Name, @{N="FolderName";E={ $_.Folder.Name}}, NumCpu, MemoryGB, ProvisionedSpaceGB, UsedSpaceGB, PowerState, OS, VMHost, @{N="Datastore";E={$_.ExtensionData.Config.DatastoreUrl.Name}}, @{N="IPAddress";E={$_.Guest.IPAddress[0]}}, @{N="DNSName";E={$_.ExtensionData.Guest.Hostname}}
}
$Output | Export-Csv C:\Temp\Result2.csv -NoTypeInformation