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

Create Organizational Units (OU) Structure in Active Directory...

May 17, 2022

Windows Security Won’t Open or Shows a Blank...

May 17, 2022

How to Manually Install Windows Updates from CAB...

May 16, 2022

RDS and RemoteApp Performance Issues on Windows Server...

May 16, 2022

Deploying Software (MSI Packages) Using Group Policy

May 12, 2022

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows 7
  • Windows Server 2019
  • Windows Server 2016
  • Windows Server 2012 R2
  • PowerShell
  • VMWare
  • Hyper-V
  • MS Office

Recent Posts

  • Create Organizational Units (OU) Structure in Active Directory with PowerShell

    May 17, 2022
  • Windows Security Won’t Open or Shows a Blank Screen on Windows 10/ 11

    May 17, 2022
  • How to Manually Install Windows Updates from CAB and MSU Files?

    May 16, 2022
  • RDS and RemoteApp Performance Issues on Windows Server 2019/2016

    May 16, 2022
  • Deploying Software (MSI Packages) Using Group Policy

    May 12, 2022
  • Updating VMware ESXi Host from the Command Line

    May 11, 2022
  • Enable or Disable MFA for Users in Azure/Microsoft 365

    April 27, 2022
  • Fix: You’ll Need a New App to Open This Windows Defender Link

    April 27, 2022
  • How to Reset an Active Directory User Password with PowerShell and ADUC?

    April 27, 2022
  • How to Completely Uninstall Previous Versions of Office with Removal Scripts?

    April 26, 2022

Follow us

woshub.com

ad

  • Facebook
  • Twitter
  • RSS
Popular Posts
  • USB Device Passthrough (Redirect) to Hyper-V Virtual Machine
  • Poor Network Performance on Hyper-V VMs in Windows Server 2019
  • Import, Export and Clone Virtual Machines in Hyper-V
  • Windows Cannot Find the Microsoft Software License Terms
  • How to Install Windows 11 on a Hyper-V Virtual Machine?
  • Hyper-V: Enabling Routing Between Internal Networks (Subnets)
  • Configure SR-IOV for Hyper-V Virtual Machines on Windows Server
Footer Logo

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


Back To Top