Saving electricity in your home lab using Powershell – part 2. Starting the previously running vms after power on


In a part 1, I showed you how you can automatically power off your hyperv-lab using a scheduled task and Powershell, I modified the original script in that blog post to also write the current running VM’s to a text file every time it runs. That blog post was inspired by MVP Rory Monaghan in order to save money on our electricity bills at a time when energy costs are at an all time high.

part 1. Automating graceful shutdown
part 2. Starting the previously running vms after power on <- you are here
part 3. Using WOL to wake the lab on demand

Now that we have this automated, wouldn’t it be nice if the VM’s that were previously running before we powered off the hyperv-lab would automatically start when we next power on the host ?

To complete this we’ll create another scheduled task called Start previously saved VMs again running in SYSTEM context as shown below.

Set the Trigger to run at System Startup

The Actions should start a program (powershell.exe) using the -file command pointing to our new Powershell script.

You might want to set some retry settings…

Here’s the new script called Start each VM from a text file.ps1

# use this to run as a scheduled task (highest permissions) to 'start' any previously running hyper-v virtual machines that were running when we did the scheduled task to save electricity
# Niall Brady 2022/08/10
# to do, add logging
# version: 0.1 2022/08/10 script created

$whichvmstostart = "C:\RunningVirtualMachines.txt"
if ([System.IO.File]::Exists($whichvmstostart)){
$vmstostart = gc $whichvmstostart

try {

ForEach($vm in $vmstostart) {

##do stuff
get-vm $vm | where state -eq 'saved' | start-vm

catch {write-host "oops something went wrong"

# delete the text file now we are done
write-host "removing '$whichvmstostart' text file"
Remove-Item -Path $whichvmstostart

write-host "The '$whichvmstostart' text file was not found, exiting"


That’s it !

So now when your Hyper-v lab powers off automatically using the script in part 1 here a new text file will appear on the root of C:\ called RunningVirtualMachines.txt

This file is over-written every time the first scheduled task runs, so it’s always updated.

On the next boot of your hyperv-lab, the second scheduled task will read this file and power back on each VM in the list, simple !

Once completed, it will then delete the file.

I hope you found this useful!



This entry was posted in hyper-v, PowerShell. Bookmark the permalink.

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.