How to enable Sleep/Hibernate on a Hyper-V guest VM?

How do I enable Sleep/Hibernate on a Hyper-V guest VM?

Notes:

  • I know I can pause/save the VM from Hyper-V. However, I want to simulate how sleep/hibernate would work on a physical machine - special notifications etc.
  • I know to enable hibernation by running powercfg -h on, but that fails with The system firmware does not support hibernation.
  • My Hyper-V is running on Windows 10 1903, as are my guest VMs.

Background: I'm developing Windows software that has special handling when a machine goes to sleep/hibernate. The software is intended for physical machines (laptops etc.), but for convenience I want to test it on Hyper-V VMs.


  1. Disable dynamic memory on the VM - it's incompatible with hibernation
  2. Run the following PowerShell script on the host:
Param (
    [string]$VmName,
    [bool]$Enable = $true
)

# To modify, machine must be off
$Vm = Get-VM -Name $VmName
$Vm | Stop-VM -Force -WarningAction Ignore

$wmiComputerSystem = gwmi -namespace root\virtualization\v2 -query "select * from Msvm_ComputerSystem where ElementName= '$VmName'"
$wmi_vsSettingData = $wmiComputerSystem.GetRelated("Msvm_VirtualSystemSettingData","Msvm_SettingsDefineState",$null,$null, "SettingData", "ManagedElement", $false, $null)
 
Write-Output ("Before: EnableHibernation = " + $wmi_vsSettingData.EnableHibernation)

# $wmi_vsSettingData.EnableHibernation = $Enable  # Doesn't work - says The property 'EnableHibernation' cannot be found on this object
# So, need to munge XML ourselves
[xml]$vsSettingsDataXml = $wmi_vsSettingData.gettext(1)
$EnableHibernationNodes = $vsSettingsDataXml.SelectNodes("/INSTANCE/PROPERTY[@NAME='EnableHibernation']")
$EnableHibernationNodes[0].VALUE=$Enable.ToString()

$wmi_vsSettingDataMgmt = Get-WmiObject -Namespace "root\virtualization\v2" -Class Msvm_VirtualSystemManagementService
$job = $wmi_vsSettingDataMgmt.ModifySystemSettings($vsSettingsDataXml.OuterXml)

Notes:

  • Tested on Windows 10 1903, both host and guest
  • Once you run this, you can do the following in the VM:
    • powercfg -a shows that Hibernate is available on the system
    • powercfg -h on works
    • Power Settings => Additional power settings => Choose what to power button does => Now you can check "Hibernate - Show in Power menu", and you'll get hibernate in the Start=>Power menu.
    • Shutdown /h will hibernate
  • After you hibernate and restart the machine, you won't be able to connect to it using "Enhanced Session". This is resolved after a full restart of the VM.