PowerShell: Enable CPU compatibility Hyper-v

We have a large VM environment with thousands of VM's. Enabling this via the GUI would have taken me months. This script checks the VM's. If it's not enabled it shuts down the VM, enables the option then boots it.

$VPSS = Get-VM | Where State -eq 

foreach ($VPS in $VPSS)
    {
    $VPSname = $VPS.VMName
    $now = get-date -Format g
    Write-Host "$now :Working on : $VPSname" 
    $cpustat = get-VMProcessor $VPSname 

    if ($cpustat.CompatibilityForMigrationEnabled -eq $False)
        {
        Stop-VM $VPS
        Set-VMProcessor $VPS -CompatibilityForMigrationEnabled 1
        Start-VM $VPS
        $now = get-date -Format g
        Write-Host "$now :$VPSname : Done" 
        Start-Sleep 5
        }    
else 
    {
    $now = get-date -Format g
    Write-Host "$now :$VPSname : No change required"
    }
}	

Occasionally you may get a VM that won't shut down gracefully. If that happens and the VM is stuck in shutting down, you need to do this:-

https://community.spiceworks.com/how_to/528-killing-ending-a-hyper-v-virtual-machine-that-is-stuck

edit : I have added a where statement at the beginning of this script to prevent it from affecting VM's that were not originally powered on. Useful if you have lot's of Veeam replicas flying around.