PowerShell’s Docs PowerShell Hyper-V Management Start and stop a Hyper-V VM with PowerShell

In my last article, we created a new VM for Kali Linux, mounted the ISO file to its virtual DVD drive, and changed the boot order so that the VM could boot from the virtual DVD drive. Here, I will show how to start and stop a Hyper-V with PowerShell. You'll also learn how to disable secure boot with PowerShell.

Start a Hyper-V VM

To start a Hyper-V VM, use the following command:

Start-VM -VMName "Kali Linux" -Passthru | Get-VM
Start a Hyper V VM with PowerShell

Start a Hyper V VM with PowerShell

This command starts the Kali Linux VM. By default, the Start-VM cmdlet does not return any output. So, I used the -PassThru parameter to generate the output and pass it down the pipeline, and then used the Get-VM cmdlet to return the status of the VM. You can see that the VM state is now Running.

Disable secure boot

Note that if you actually try to boot the VM from the DVD drive to start the OS installation, it will fail. This is because the Generation 2 VMs have "Secure Boot" enabled by default.

Secure boot is a security feature available in the UEFI firmware that is designed to ensure that an operating system's loading utility is safe and has not been tampered with. To install Kali Linux or any other Linux distro in the Hyper-V VM, you might need to disable the secure boot feature of the VM. If you're going to install Windows 10 or 11, you can keep secure boot enabled.

To disable secure boot for a VM in Hyper-V, make sure the VM is stopped, and then run the following command:

Set-VMFirmware -VMName "Kali Linux" -EnableSecureBoot Off
Disable Secure Boot for Hyper V VM using PowerShell

Disable Secure Boot for Hyper V VM using PowerShell

This command disables the secure boot for our Kali Linux VM. You can now go ahead and deploy the operating system on the VM. I am not going to cover the OS deployment steps in this guide.

Stop a Hyper-V VM

To stop a Hyper-V VM, use the following command:

Stop-VM -VMName "Kali Linux"
Stop a Hyper V VM with PowerShell

Stop a Hyper V VM with PowerShell

This command shuts down the VM through the guest operating system. If the VM is not responding or there is some other issue, use the -Force parameter with the Stop-VM cmdlet to forcefully stop the VM. Please remember that forcefully stopping a VM could result in data loss. To save the VM state and stop it, use the -Save parameter as shown below:

Stop-VM -VMName "Kali Linux" -Save
Save the VM state using PowerShell

Save the VM state using PowerShell

This command saves the VM state before stopping it. When you start the VM again, it resumes from the saved state.

The Stop-VM cmdlet also supports the -TurnOff parameter, which is equivalent to unplugging the power cord from the system.

So, what's the difference between the -Force and -TurnOff parameters?

The -Force parameter is safer since it invokes the guest operating system's shutdown functionality. It usually gives the VM five minutes so that the end user can safely close the running programs and save the data.

The -TurnOff parameter, on the other hand, is equivalent to unplugging the power cord from the system, which forcefully turns off the VM. The user gets no time to close the apps and save the data, which could result in data loss, disk errors, or even disk failures. Use the -TurnOff parameter only as a last resort when -Force does not work.

Articles in seriesManage Hyper-V with PowerShell

Discussion (0)

There are no comments for this doc yet.

Leave a Reply

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

Articles in seriesManage Hyper-V with PowerShell
© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account