Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Virtualization / Hyper-V / Managing Hyper-V Virtual Machines with PowerShell

June 8, 2023 Hyper-VPowerShellVirtualizationWindows 10Windows Server 2019

Managing Hyper-V Virtual Machines with PowerShell

This article is about managing Hyper-V virtual machines from the PowerShell console. We’ll look at how to create virtual switches and virtual machines, change VM settings, and manage VMs. You can use these commands to manually manage your Hyper-V VMs or in your PowerShell scripts to automate various tasks.

Contents:
  • How to Enable Hyper-V Role on Windows Server and Windows 10/11
  • Create a Hyper-V Virtual Switch with PowerShell
  • Create a Hyper-V Virtual Machine Using PowerShell
  • How to Manage Hyper-V VMs with PowerShell?

How to Enable Hyper-V Role on Windows Server and Windows 10/11

To install the Hyper-V role, a host must have a SLAT-enabled CPU with virtualization support. On Windows Server, the following PowerShell command is used to install the Hyper-V role:

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

In desktop editions (Windows 10 and 11), the Hyper-V role is installed as follows:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V –All

To manage a Hyper-V host, the Hyper-V PowerShell module must be installed on the computer. You can display a full list of available commands (they depend on your Windows version) as follows:

Get-Command -Module hyper-v

Hyper-V Module in Windows PowerShell

In Windows Server 2022, there are 245 cmdlets available in the Hyper-V module.

To display a list of Hyper-V host settings, use the command below:

Get-VMHost|fl *

To show only the information about the number of available CPU cores and RAM:

Get-VMHost| select LogicalProcessorCount, MemoryCapacity

find out available CPU and memory in hyper-v

Use the Set-VMHost to change Hyper-V host settings. The following command will change the default paths to virtual disks and VM configuration files:

Set-VMHost -VirtualMachinePath E:\VMs -VirtualHardDiskPath E:\VMs\VHD'

Create a Hyper-V Virtual Switch with PowerShell

First of all, create a virtual switch on your Hyper-V host. Virtual machines can only access the network through a virtual switch.

Let’s display a list of available physical network adapters on the Hyper-V host:

Get-NetAdapter | where {$_.status -eq "up"}

If your server supports SR-IOV (Single-Root Input/Output (I/O) Virtualization), note that you should enable the option when creating your switch. You cannot enable SR-IOV for an existing vSwitch. Learn more about how to enable SR-IOV support for Hyper-V VMs.

Create an external virtual switch:

New-VMSwitch -Name "ExternalVMSwitch" -AllowManagementOS $True -NetAdapterName Ethernet0 -SwitchType External

New-VMSwitch - create a Hyper-V Virtual Switch

Create a Hyper-V Virtual Machine Using PowerShell

To create a new Hyper-V virtual machine, the New-VM cmdlet is used. In this example, we will create a new Generation 2 VM with 1GB RAM and a 10 GB VHDX disk.

$VMName = "mun-prx2"
$VM = @{
Name = $VMName
MemoryStartupBytes = 1Gb
Generation = 2
NewVHDPath = "E:\HV\$VMName\$VMName.vhdx"
NewVHDSizeBytes = 10Gb
BootDevice = "VHD"
Path = "E:\HV\$VMName"
SwitchName = "ExternalVMSwitch"
}
New-VM @VM

hyper-v create vm powershell

Here is an example of using PowerShell to create a Windows 11 virtual machine on Hyper-V.

Let’s look at the commands you can use to change VM settings.

To increase RAM size for a VM:

Get-VM -Name mun-prx2| Set-VMMemory -StartupBytes 2Gb

To change the number of vCPUs:

Set-VMProcessor mun-prx2 -Count 2

Enable automatic startup for a Hyper-V virtual machine:

Get-VM –VMname mun-prx2 | Set-VM –AutomaticStartAction Start

To connect an additional virtual disk to the VM, create the vhdx file first:

New-VHD -Path 'C:\VM\new-prx2-drive.vhdx' -SizeBytes 2GB

And then connect it to your VM:

Add-VMHardDiskDrive -VMName mun-prx2 -Path 'C:\VM\new-prx2-drive.vhdx'

How to Manage Hyper-V VMs with PowerShell?

To display a list of virtual machines on a Hyper-V host:

Get-VM

hyper-v list vm powershell

The command returns the list of VMs with some basic properties. To display all VM properties, run the command below:

Get-VM -Name mun-dmz1 | fl *

To show running VMs only:

Get-VM | where {$_.State -eq 'Running'}

To start a virtual machine:

Start-VM -Name mun-app01

To start all stopped virtual machines:

Get-VM | where {$_.State -eq 'Off'} | Start-VM

Shutdown the VM (correct shutdown using Integration Services in the guest OS):

Stop-VM -Name mun-app01

To power off the VM, the TurnOff option is used:

Stop-VM -Name mun-app01 –TurnOff

Learn how to stop an unresponsive Hyper-V VM.

Mount the ISO file to a virtual CD/DVD device:

Set-VMDvdDrive -VMName mun-app01 -Path c:\iso\WinSrv2022.iso

You can use the USB passthrough to redirect the USB device (media/drive) to a Hyper-V VM.

To move all VM files to another disk on the fly, use the command below:

Move-VMStorage mun-app01 -DestinationStoragePath D:\VM\mun-app01

You can extend or shrink a virtual disk file using the Resize-VHD cmdlet:

Resize-VHD -Path 'C:\VM\mun-fs01.vhdx' -SizeBytes 50Gb

To create a checkpoint (snapshot) of a VM:

Get-VM -Name mun-app01| Checkpoint-VM -SnapshotName "Before upgrading SAP"

To display a list of available checkpoints:

Get-Vm -Name mun-app01| Get-VMCheckpoint

Manage checkpoints in Hyper-V with PowerShell

To restore a VM to its previous state from a checkpoint:

Restore-VMCheckpoint -Name "Before upgrading SAP" -VMName mun-app01 -Confirm:$false

To remove a snapshot:

Remove-VMCheckpoint -VMName mun-app01 -Name "Before upgrading SAP"

Exporting, importing, and cloning a VM in Hyper-V is described in this article:

Export-VM -Name mun-app01 -Path 'C:\VHD\export' -CaptureLiveState CaptureCrashConsistentState

You can use the built-in Windows Server Backup (WSB) to backup Hyper-V virtual machines.

To get IP addresses of guest OS of virtual machines:

Get-VM | Select -ExpandProperty NetworkAdapters | Select VMName, IPAddresses, Status

To connect to a virtual machine console:

vmconnect.exe localhost mun-app01

VMConnect.exe Connect to a Hyper-V Virtual Machine.

You can use PowerShell Direct to connect directly to guest OSs of virtual machines through the vmbus (available in Windows Server 2016, Windows 10, and newer). Use the Invoke-Command (to run scripts) and Enter-PSSession (to run an interactive PowerShell session):

Invoke-Command -VMName mun-app01 -ScriptBlock {Get-Process}
Enter-PSSession -VMName mun-app01

To copy files from a Hyper-V host to a virtual machine using PowerShell Direct, use the following command:

$PSSession = New-PSSession -VMName mun-app01 -Credential (Get-Credential)
Copy-Item -ToSession $PSSession -Path E:\ISO\install_image.iso -Destination D:\ISO\

You can use PowerShell to manage virtual machines on Hyper-V hosts locally or remotely (on Windows Server Full GUI or Server Core modes, on free Windows Hyper-V Server, or Windows 10) together with graphical tools such as Hyper-V Manager console or Windows Admin Center.

0 comment
5
Facebook Twitter Google + Pinterest
previous post
How to Backup and Restore Websites and IIS configuration
next post
Installing an Open Source KMS Server (Vlmcsd) on Linux

Related Reading

View Windows Update History with PowerShell (CMD)

April 30, 2025

Change BIOS from Legacy to UEFI without Reinstalling...

April 21, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

Allowing Ping (ICMP Echo) Responses in Windows Firewall

April 15, 2025

How to Pause (Delay) Update Installation on Windows...

April 11, 2025

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Hyper-V Virtual Machine Stuck in Stopping/Starting State
  • How to Install and Configure Free Hyper-V Server 2019/2016
  • Poor Network Performance on Hyper-V VMs in Windows Server 2019
  • How to Install Windows 11 on a Hyper-V Virtual Machine
  • Windows Cannot Find the Microsoft Software License Terms
  • USB Device Passthrough (Redirect) to Hyper-V Virtual Machine
  • How to Enable and Configure Hyper-V Remote Management
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top