1. Home
  2. Windows Tips
  3. How to manage hyper v vms in powershell

How to manage Hyper-V VMs in PowerShell

Did you know that you can fully automate your Hyper-V VMs on Windows Server? Follow along with this in-depth guide to learn how you can unlock the power of Hyper-V automation with Windows PowerShell.

hero image for Hyper-V powershell.

How to Get Hyper-V Working on Windows Server with PowerShell

Before managing and automating Hyper-V with PowerShell on your Windows Server, you must first enable the Hyper-V virtualization platform. Thankfully, there’s an easy, PowerShell-friendly way to get Hyper-V working. To start, open up a PowerShell window as Administrator.

Once the PowerShell window is open, you can use the following Install-WindowsFeature command to turn on Hyper-V as a feature on your Windows Server.

PowerShell is enabling the Hyper-V platform on Windows Server.

Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Restart

After running the command above, Hyper-V will be enabled on your system. However, you’ll need to reboot Windows Server before you attempt to use Hyper-V. To reboot your Windows Server, you can use the following Restart-Computer command, or through the Windows Server UI.

Restart-Computer

When you log back into Windows Server, you’ll be able to use Microsoft’s Hyper-V virtualization platform with PowerShell for management and automation.

How to use PowerShell to accomplish common virtualization tasks

There are quite a few things you can do in Hyper-V right in PowerShell. Let’s start with something basic, like the “Get-VM” command. This cmdlet will list all available VMs on Windows Server Hyper-V. To enter this command, launch a PowerShell window as Administrator.

Get-VM

The Get-VM cmdlet is very basic, as it just lists what virtual machines are available. However, this will come in handy, especially if you need to know what VMs are available to manage via PowerShell Hyper-V cmdlets.

There are quite a lot of useful cmdlets you can use to accomplish Hyper-V tasks on Windows Server. The New-VM command is one of those cmdlets. To create a new VM, use the following New-VM command.

PowerShell is using the Hyper-V cmdlet to create a VM.

New-VM -Name "MyNewVM" -MemoryStartupBytes 4GB

Another useful Hyper-V cmdlet you can utilize in PowerShell is the Start-VM command. This command will allow you to quickly start up any VM through PowerShell.

Hyper-V management interface on Windows Server showing a booted Debian Linux.

Start-VM -Name "MyNewVM"

If you wish to turn off a VM you started with Start-VM, there is also Stop-VM which allows you to turn off a VM you started using the Stop-VM -Name "MyNewVM" cmdlet.

More useful commands to accomplish common virtualization tasks

In the previous section, we went over basic commands like “Get-VM,” “New-VM”, “Start-VM” and “Stop-VM.” However, there are a lot more useful commands you can use to accomplish common virtualization tasks in Hyper-V. One such command is New-VHD. This command lets you create a new virtual hard drive for Hyper-V right from PowerShell.

Hyper-V cmdlet in Windows Server is creating a virtual hard drive.

New-VHD -Path "C:\VMs\MyNewVM.vhdx" -SizeBytes 75GB -Dynamic

Creating a new VHD is one thing, but what if you want to attach it to an Existing VM? With the Add-VMHardDiskDrive cmdlet, you can. However, be sure you know the name of the VM beforehand (find out with Get-VM.)

Add-VMHardDiskDrive -VMName "MyNewVM" -Path "C:\VMs\MyNewVM.vhdx"

In addition to adding new VHDs to Hyper-V VMs in PowerShell, you can also manage networking from there too. For example, if you need to connect a VM to a virtual switch (that already exists), you can use the Connect-VMNetworkAdapter cmdlet.

Connect-VMNetworkAdapter -VMName "MyNewVM" -SwitchName "YourVirtualSwitch"

This isn’t the only networking command for PowerShell/Hyper-V. There are quite a few. Below is a list of useful commands you can take advantage of to get the most out of Hyper-V VM networking.

  • Get-VMNetworkAdapter -VMName “VMName” — This cmdlet allows you to get virtual network adapter information for your VM.
  • Set-VMNetworkAdapter -VMName “VMName” — This cmdlet allows you to configure networking adapter features.
  • Add-VMNetworkAdapter -VMName “VMName” -SwitchName “VirtualSwitch” — This cmdlet allows you to add a networking adapter to a VM.
  • Remove-VMNetworkAdapter -VMName “VMName” -Name “AdapterName” — If you wish to remove a networking adapter from a VM, you can do that with this cmdlet.
  • New-VMSwitch -Name “VMName” -NetAdapterName “NetworkAdapterName” — If you need to create a new virtual switch on a VM, you can do so with the following cmdlet.
    – Get-VMSwitch -Name “VMName” — If you need information on your virtual switch, this cmdlet will do it for you.
    – Remove-VMSwitch -Name “VMName” — Need to get rid of a virtual switch from a VM in Hyper-V? Do it with this cmdlet.
    – Set-VMSwitch -Name “VMName” -NetAdapterName “AdapterName” — This cmdlet allows you to set the name of a virtual switch.
    – Set-VMNetworkAdapterVlan -VMName “VMName” -Access -VlanId 10 — If you need to configure VLAN in a Hyper-V VM, this cmdlet lets you do it.

How to automate Hyper-V tasks with PowerShell scripts

Knowing the basic cmdlets for Hyper-V in PowerShell will allow you to write some powerful automation scripts that will make it easier to manage VMs. For example, if you need to reboot 3 VMs every week on Tuesday after patches, you could write the following .PS1 script.

Note: before attempting to use this script, change “VM1,” “VM2,” and “VM3” to the VMs you wish to interact with via the script.

# PowerShell script to reboot VMs

# Define the VM names
$vmNames = @("VM1", "VM2", "VM3")

# Function to reboot VMs
Function Reboot-VMs {
    foreach ($vmName in $vmNames) {
        Restart-VM -Name $vmName -Force
        Write-Host "Rebooted VM: $vmName"
    }
}

# Schedule the script to run every Tuesday after patching
# Note: This part is usually done outside the script through Task Scheduler or similar tools

# Call the function to reboot VMs
Reboot-VMs

PowerShell scripting can be advanced or basic, it really depends on your talents. Mastering PowerShell scripting allows you to automate a wide range of Hyper-V operations. For more information on how you can learn how to create PowerShell scripts, visit the official Microsoft PowerShell scripting documentation.

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.