Functions/Optimize-HyperVFolder.ps1
function Optimize-HyperVFolder { $HyperVFolder = "C:\Hyper-V" $VMs = Get-VM foreach($VM in $VMs) { $VM | Select-Object Name, Path $CorrectPath = "$HyperVFolder\$($VM.Name)" if($VM.Path -ne $CorrectPath) { Move-VMStorage -VMName $VM.Name -DestinationStoragePath $CorrectPath -Verbose } } $VHDFolder = "$HyperVFolder\Virtual Hard Disks" $VMFolder = "$HyperVFolder\Virtual Machines" if(Test-Path $VHDFolder) { if((Get-ChildItem $VHDFolder -Recurse).length -eq 0) { Remove-Item $VHDFolder } } if(Test-Path $VMFolder) { if((Get-ChildItem $VMFolder -Recurse).length -eq 0) { Remove-Item $VMFolder } } } |