Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

March 15, 2022 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 or Windows Admin Center.

0 comment
2
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

How to Run Program without Admin Privileges and...

March 24, 2023

Configure Network Settings on Windows with PowerShell: IP...

March 24, 2023

Exchange Offline Address Book Not Updating in Outlook

March 21, 2023

Attaching Host USB Devices to WSL or Hyper-V...

March 20, 2023

Print Screen Key Not Working in Windows

March 17, 2023

Leave a Comment Cancel Reply

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

  • How to Run Program without Admin Privileges and Bypass UAC Prompt?

    March 24, 2023
  • Configure Network Settings on Windows with PowerShell: IP Address, DNS, Default Gateway, Static Routes

    March 24, 2023
  • Exchange Offline Address Book Not Updating in Outlook

    March 21, 2023
  • Attaching Host USB Devices to WSL or Hyper-V VM

    March 20, 2023
  • Sending an E-mail to a Microsoft Teams Channel

    March 17, 2023
  • How to Restore Deleted Users in Azure AD (Microsoft 365)?

    March 16, 2023
  • Fix: Remote Desktop Services Is Currently Busy

    March 15, 2023
  • Send-MailMessage: Sending E-mails with PowerShell

    March 14, 2023
  • Clear Cache and Temp Files in User Profiles on Windows (RDS) with PowerShell and GPO

    March 13, 2023
  • Prevent Users from Creating New Groups in Microsoft 365 (Teams/Outlook)

    March 6, 2023

Follow us

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

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


Back To Top