MOCs

Overview

Hyper-V is Microsoft’s hardware virtualization product. It lets you create and run a software version of a computer, called a virtual machine. PowerShell provides a command-line shell and scripting language to automate tasks for Hyper-V management.

Documentation

Documenation
PowerShell docs
My HyperV Powershell code

Tips and Tricks

  • Exporting a VM:
Export-VM -Name "VMName" -Path "C:\VMExports"
  • Importing a VM:
Import-VM -Path "C:\VMExports\VMName\Virtual Machines\VMID.xml"
  • Configuring VM Automatic Start and Stop Actions:
Set-VM -Name "VMName" -AutomaticStartAction StartIfRunning -AutomaticStopAction Save

Installing Hyper-V

Hyper-V can be installed on Windows Server and some editions of Windows 10 and Windows 11. Use the following PowerShell command to install Hyper-V on your system:

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

Virtual Machine Management

  • Creating a New Virtual Machine:
New-VM -Name "VMName" -MemoryStartupBytes 2GB -NewVHDPath "C:\VMs\VMName.vhdx" -NewVHDSizeBytes 50GB -Generation 2
  • Starting a Virtual Machine:
Start-VM -Name "VMName"
  • Stopping a Virtual Machine:
Stop-VM -Name "VMName"

Virtual Hard Disk Management

  • Creating a New Virtual Hard Disk:
New-VHD -Path "C:\VMs\NewDisk.vhdx" -SizeBytes 100GB
  • Attaching a Virtual Hard Disk to a VM:
Add-VMHardDiskDrive -VMName "VMName" -Path "C:\VMs\NewDisk.vhdx"

Virtual Network Management

  • Creating a Virtual Switch:
New-VMSwitch -Name "VMSwitch" -NetAdapterName "Ethernet" -AllowManagementOS $true
  • Listing Virtual Switches:
Get-VMSwitch

Snapshot Management

  • Creating a Snapshot (Checkpoint):
Checkpoint-VM -Name "VMName" -SnapshotName "SnapshotName"
  • Restoring a Snapshot (Checkpoint):
Restore-VMSnapshot -VMName "VMName" -Name "SnapshotName"

Checking Hyper-V Status

  • List All VMs and Their Status:
Get-VM
  • Check Hyper-V Feature Status:
Get-WindowsFeature Hyper-V