Try it like this
New-VIProperty-Name'FolderPath'-ObjectType'VirtualMachine'-Value {
param($vm)
$path=@()
$excludeFolders="Datacenters","vm"
$parent=$vm.ExtensionData
while($parent.Parent){
$parent=Get-View$parent.Parent
if($excludeFolders-notcontains$parent.Name){
$path+=$parent.Name
}
}
[string]::Join('/',$path[($path.Count-1)..0])
} -Force-BasedOnExtensionProperty'Parent'|Out-Null
Get-VM|Group-ObjectFolderPath|%{
New-ObjectPSObject-Property@{
Consumer=$_.Name
"No. of Virtual Machines"=$_.Group.Count
"Allocated CPU"=$_.Group|Measure-Object-PropertyNumCPU-Sum|Select-ExpandPropertySum
"Allocated Memory"=$_.Group|Measure-Object-PropertyMemoryGB-Sum|Select-ExpandPropertySum
"Disk Provisioned"=$_.Group|Measure-Object-PropertyProvisionedSpaceGB-Sum|Select-ExpandPropertySum
}
}
It uses a VIProperty to get the blue folderpath, and then groups the VM on this property.