Shutdown your Windows 10 Hyper-V VM when it’s idle, Easy!

Hyper-V on Windows 10 Pro is a great way of running a separate virtual instance of an operating system. Whilst keeping your normal day to day environment safe and secure for anything in the VM.

Sure Windows Sandbox is great for testing out little things, but when you need a more permanent solution I feel like a VM on my laptop is the way to go.

I personally run a Windows 10 VM on my laptop for connecting to customer environments for a few reasons,

  • Most clients have their own VPN solution and these typically don’t play well with each other. Constantly fiddling with the networking stack in Windows
  • Some customers VPN solution blocks all other forms of Internet connectivity making working in the cloud a pain
  • I don’t want anything on my daily driver to affect the customer’s environments and vice versa.
  • Some places even make me enrol my machine into their MDM policy. I’d rather just give them control over their own environment rather than my whole laptop.

One of the issues with this is that I start up my VM’s at the customer site, use them all day and when I’m done I close my laptop and jump on a tram/train. When I get home I notice my laptop has become a thermonuclear heater as the VM has woken it from sleep for the VPN client to complain about it losing Internet connectivity or something else mundane.

Not only this, but the VM is a ton of extra CPU load for the machine. This is fine when I’m plugged in. But if it’s on battery it’s an extra drain. And if I’m using my old surface it’s an extra thermal load that causes the CPU the thermal throttle.

The thing is, most of the time I’m just connecting to a customer environment. Running a few commands and then spending the next few hours in word writing proposals.

What if there was an easy way to stop the VM when it’s not in use?

Well there is, and the magic of it is that Hyper-V by default connects a virtual RDP session between the Host computer and the Guest OS.

We can leverage that, along with windows task manager to Shut the machine down.

Open up Task Scheduler, right click on the Task Scheduler Library and click
Create task… (not a basic task)

Give the task a name and set it to run when the user is logged in or not, and with the highest privileges.

Over on the triggers tab, click New… and in the new window, change the Begin the task: option to On disconnect from user session and click OK.

Now on the Actions tab, click New… and simply type shutdown in the program/script field to call the built in Windows shutdown.exe you can specify any command line arguments you like, but I would recommend -t 0 -s. Then click OK twice. Entering your admin password when prompted.

Close the Hyper-V window? Bam, VM shuts down

What if I close the window by mistake? I lose all my work!

That’s an easy fix. We can take this a step further if we want a grace period of a few minutes when we close the Hyper-V window. Simply grab the great dontsleep.exe from SoftwareOK and configure it to Please Sleep like below.

You will need to click the little # icon in the middle to switch to shutdown instead of just sleeping.

Your settings will be saved in an INI file in the same folder as the EXE so there is no need to worry about command-line settings.

Now instead of telling the task manager to shutdown, instead we ask it to call dontsleep.exe when the RDP session ends.

Note, you also need to change the task to run as you and only when you’re logged in, this enables you to cancel it easily.

Now we get a grace period of 5 minutes to login and close dontsleep before the VM shuts down. Just click Exit in the bottom right corner.

I hope that helps someone not turn their back back into a very small heater or maybe prevents you from having a flat battery for that client meeting!

Till next time, why not go and check out some of my Skype and Teams tools?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.