4

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 Answer 1

3
  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.
8
  • is there anything besides dynamic memory which could prevent hibernate option on the Guest VM? I keep getting the message "Hibernate The system firmware does not support hibernation." olthough the host does.
    – AndreiM
    Commented Jul 7, 2020 at 13:45
  • You need to run the powershell on the host. I've edited the answer to make this clearer (I hope).
    – Jonathan
    Commented Jul 8, 2020 at 15:57
  • Thanks, now it makes more sense. Do you have any magic script for enabling S1 and S3 power states?
    – AndreiM
    Commented Jul 9, 2020 at 12:04
  • 1
    Worked great for me; lots of WHQL tests require hibernate/suspend to be enabled, so this makes it possible to use VMs for those (software testing, not hardware). Commented May 3, 2021 at 16:55
  • @AndreiM: Sorry, I don't see any sleep settings in there. Maybe Hyper-V doesn't support that at all - This seems to indicate that.
    – Jonathan
    Commented Aug 1, 2021 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .