The answer powershell cmdlet is used to change the configuration of a virtual machine.

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.

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

The answer powershell cmdlet is used to change the configuration of a virtual machine.

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

The answer powershell cmdlet is used to change the configuration of a virtual machine.

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"}

Create an external virtual switch:

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

The answer powershell cmdlet is used to change the configuration of a virtual machine.

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

The answer powershell cmdlet is used to change the configuration of a virtual machine.

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

The answer powershell cmdlet is used to change the configuration of a virtual machine.

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

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

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

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

The answer powershell cmdlet is used to change the configuration of a virtual machine.

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

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

The answer powershell cmdlet is used to change the configuration of a 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.

You plan to implement guest clustering.You review the following information: Whoami results, as shown in the Whoami Command exhibit (Click the Exhibit button.)

The answer powershell cmdlet is used to change the configuration of a virtual machine.

System information settings, as shown in the System Information Settings exhibit (Click the Exhibit button.)

The answer powershell cmdlet is used to change the configuration of a virtual machine.

The Add Roles and Features Wizard, as shown in the Features Wizard exhibit (Click theExhibit button.)

The answer powershell cmdlet is used to change the configuration of a virtual machine.

You need to ensure that you can implement guest clustering.For each of the following statements, select Yes if the statement is true. Otherwise, selectNo. Each correct selection is worth one point.

The answer powershell cmdlet is used to change the configuration of a virtual machine.