How to Configure VM Auto Start with PowerShell

 

Configuring auto start on VMs is very important, especially for smaller companies that don’t have the budget for a power-outage-proof solution. I’ve seen it a few times when a power outage occurred over the weekend and the IT admin was scrambling to get all the VMs powered back on. Configuring auto start on each VM can easily be overlooked, but with a little PowerShell know how, managing and configuring this setting is a breeze.

How to Find Out which VMs are not Configured for Auto Start

PowerShell can allow you to see how auto start is configured for each VM within seconds. Instead of right-clicking on each and every VM to see their settings, just open up PowerShell on the Hyper-V host and use the Get-VM cmdlet piped to Select-Object:

Get-VM –VMname * | Select-Object VMname,AutomaticStartAction

1-list VM

This will display an easy-to-view list of all VMs on the host and show what their automatic start action is set for. In our example we see all the VMs are configured with the automatic start action of “nothing”. Meaning, they will stay powered off once the Hyper-V host is powered back on. But, thanks to the auto start feature, we can easily choose and configure which VMs we want to start up automatically.

How to Configure Automatic Start

Now that we know how each VM is configured, we can use PowerShell to modify the setting [as necessary]. The syntax is pretty simple. We use the Get-VM cmdlet and pipe it to Set-VM and use the –AutomaticStartAction parameter. With this parameter, there are three possible values:

  • Nothing – This one is pretty obvious. When the host is powered on, the VM will do nothing.
  • StartIfRunning – This setting will start the VM only if it was running when the host was stopped.
  • Start – This will cause the VM to always start up automatically no matter what state the VM was in previously.

In this example we will configure all VMs to always start up automatically so we will use the “Start” value:

Get-VM –VMname * | Set-VM –AutomaticStartAction Start

 2 - Configure VM

Now we can run the previous command to verify the configuration has changed on all VMs.

3 - Check Config

 

How to Configure the Automatic Start Delay

In a windows environment, it is a best practice to have domain controllers start up first before the other VMs. This allows for fewer hiccups with VMs running AD-sensitive applications, such as Exchange. In this example, we will configure our VMs that are not domain controllers to start up after 120 seconds. The domain controllers are conveniently named with a format that allows us to distinguish them (TGLAB-DC01,TGLAB,DC02, and TGLAB-DC03). So, in order to tell PowerShell to only select the non domain controller servers, we will simply use the Where-Object cmdlet along with the –FilterScript parameter to filter our list by the VMNAME property. This allows us to narrow the list to VMs that don’t follow our domain controller naming scheme:

Get-VM –VMname * | Where-Object –FilterScript {$.VMname –notlike “tglab-dc*”}

The result should be a list of all non-domain controller VMs on our host.

4 - filter

Now comes the easy part. We just pipe all this information we obtained to Set-VM and use the –AutomaticStartDelay parameter followed by how many seconds we want the VMs to wait before powering on. In this example, we will set the delay to 120 seconds:

Get-VM –VMname * | Where-object –FilterScript {$_.vmname –notlike “tglab-dc*”} | Set-VM –AutomaticStartDelay 120

5 - filter config

Now run the following command to view both the automatic start action and start delay of each VM:

Get-VM –VMname * | Select-Object VMname,AutomaticStartAction,AutomaticStartDelay

6 - check all

We now have our DCs powering on instantly with all other VMs waiting 120 seconds to power on.

How to Configure Auto Start with Multiple Hyper-V Hosts

When working with multiple Hyper-V hosts, you would just incorporate the –ComputerName parameter when using the Get-VM cmdlet. For example, if I had two hosts (TGLAB-HV01 and TGLAB-HV02) and I wanted to list the automatic start information for both hosts, I would use the following syntax:

Get-VM –computername tglab-hv01,tglab-hv02 –VMname * | Select-Object VMname,AutomaticStartAction,AutomaticStartDelay

7- check all hosts

If you have a long list of Hyper-V hosts, you can use splatting to make it easier to specify all hosts. Simply create a hash table with the –ComputerName parameter as the key and the Hyper-V hosts you’d like to check as the values:

$hosts = @{ComputerName = "tglab-hv01","tglab-hv02"}

Next, when using Get-VM, instead of using the –ComputerName parameter, use the “@” symbol followed by the variable name used for the hash table (@hosts). This will pass the -ComputerName parameter along to the Get-VM command and include all Hyper-V hosts listed:

Get-VM @hosts –VMname * | Set-VM –AutomaticStartAction start

Get-VM @hosts –vmname * | Select-Object VMname,AutomaticStartAction,AutomaticStartDelay

8 - check hosts splat

In just three simple lines we can modify the auto start action for all VMs on all hosts.

 

Altaro Hyper-V Backup
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

15 thoughts on "How to Configure VM Auto Start with PowerShell"

  • Davorin says:

    Hi,
    how to use autostart with a Hyper-V Cluster?
    We have a cluster of three hosts and I find it impossible to configure autostart with a delay,
    Regards
    Davorin

    • Luke Orellana says:

      Do you have an issue with Autostart completely working at all? or just configuring the VM to start up with a delay?

      If there is an issue with the storage configuration, it can cause the host to boot back up before storage is initiated, preventing the autostart process from occurring.

      • Dinh Trong Thinh says:

        Hello, thank for your topic,
        But about cluster, could you explain detail about configure the VM startup with delay? or without delay also.

        • Luke Orellana says:

          Absolutely! Nirmal Sharma does a great explanation of this on his post Hyper-V Automatic Start and Stop Action. Be sure to check it out.

          • Dinh Trong Thinh says:

            Thanks for support me, I checked it but seem it is perfect only for Standard host, not in cluster. In cluster, the recommended Start option will be nothing.
            I t will be configure by Cluster management screen. but I will try change to delay setting, and it is not work. I looking up in many topic but until now, not found any good resource.

  • Davorin says:

    Hi,
    how to use autostart with a Hyper-V Cluster?
    We have a cluster of three hosts and I find it impossible to configure autostart with a delay,
    Regards
    Davorin

  • Mike Graham says:

    Hi,

    I’m about to impliment this on around 10 Hyper-V Hosts.

    In my environment, we have started to suffer from ‘Bootstorms’ – Too many VM’s starting at once and the host does not have enough HDD IO’s to support this action. Critical services on the VM’s fail and from a host being completely off, to having all vm’s operational can take 20-30 minutes.

    In half of my Hosts Iv’e moved to SSD Drives. Bootstorming is no longer and issue, and from a host being off, to all vms operational is under 2 mins 🙂

    For the Hosts that still have traditional drives – Is there a powershell script to ‘Stagger’ the Autostart of each VM by say 1 minute, so as to avoid a boot storm. I know this can be done manually, but I have that many VM’s coming and going that If I could rerun a script now and then to stagger them automatically It would be fantastic.

    Machines are IBM x3650, 2TB RAID10 (8 Drives) Server 08/12, 192GB Per server roughly. Identical machines almost, drived being only the difference, SSD i great, HDD Fails. Around 30-40 VM’s per host.

    Has anyone else run into this issue ? Thoughts ?

    • A simplified way of doing this is by using the get-random cmdlet with the -maximum parameter to specify the highest delay time you would like to implement.

      For example: ForEach ($vm in (get-vm *)) {Set-VM $vm -automaticstartdelay (Get-Random -maximum 500) -automaticstartaction start}

      That’s why it’s important to provision storage according to the IOPs consumed of each VM. When dealing with performance issues on Hyper-V hosts in general, there’s usually a very high chance it’s I/O related.

  • Mike Graham says:

    Hi,

    I’m about to impliment this on around 10 Hyper-V Hosts.

    In my environment, we have started to suffer from ‘Bootstorms’ – Too many VM’s starting at once and the host does not have enough HDD IO’s to support this action. Critical services on the VM’s fail and from a host being completely off, to having all vm’s operational can take 20-30 minutes.

    In half of my Hosts Iv’e moved to SSD Drives. Bootstorming is no longer and issue, and from a host being off, to all vms operational is under 2 mins 🙂

    For the Hosts that still have traditional drives – Is there a powershell script to ‘Stagger’ the Autostart of each VM by say 1 minute, so as to avoid a boot storm. I know this can be done manually, but I have that many VM’s coming and going that If I could rerun a script now and then to stagger them automatically It would be fantastic.

    Machines are IBM x3650, 2TB RAID10 (8 Drives) Server 08/12, 192GB Per server roughly. Identical machines almost, drived being only the difference, SSD i great, HDD Fails. Around 30-40 VM’s per host.

    Has anyone else run into this issue ? Thoughts ?

Leave a comment or ask a question

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

Your email address will not be published.

Notify me of follow-up replies via email

Yes, I would like to receive new blog posts by email

What is the color of grass?

Please note: If you’re not already a member on the Dojo Forums you will create a new account and receive an activation email.