If you are using Microsoft’s Virtualization Platform, you may be familiar with graphical management tools such as Hyper-V Manager, System Center Virtual Machine Manager, and Failover Cluster Manager. Besides these, PowerShell was and still remains a viable method to achieve the same level of control and to further automate and reduce the time of repetitive tasks.
Why do I use PowerShell? Automation, reducing the time of repetitive tasks, and mostly because I enjoy using and learning PowerShell!
I managed to put down a list containing all the essential PowerShell one-liners that will serve you well in your day-to-day operations.
Prerequisites
To use any of the listed cmdlets to start managing Hyper-V Virtual Machines, you will need a system running Windows Desktop or Server OS along with the following:
- Hyper-V Management Tools
- PowerShell or PowerShell Core
- Administrative Access to Hyper-V Host
Get Started
Install Hyper-V Management Tools
The following cmdlet will enable Hyper-V Management Tools.
Enable-WindowsOptionalFeature -FeatureName 'Microsoft-Hyper-V-Tools-All' -Online
Create Virtual Machine with a new blank Virtual Hard Disk
In this example a new Generation 2 Virtual Machine will be created, under the name MyVM, with 4 GB of RAM, Configuration files will be placed under path C:\VMS\ to a new folder that has the same name as the VM, a new VHDX disk sized at 60GB will be created under the same path and the VM’s NIC will be connected to the Virtual Switch called “External Virtual Switch”.
New-VM -Name "MyVM" -MemoryStartupBytes 4GB -Generation 2 -Path "C:\VMS\" -NewVHDPath "C:\VMS\MyVM\disk.vhdx" -NewVHDSizeBytes 60GB -SwitchName "External Virtual Switch"
Create Virtual Machine with an existing Virtual Hard Disk
In this example a new Generation 2 Virtual Machine will be created, under the name MyVM, with 4 GB of RAM, Configuration files will be placed under path C:\VMS\ to a new folder that has the same name as the VM, an existing VHDX will be used and the VM’s NIC will be connected to the Virtual Switch called “External Virtual Switch”.
New-VM -Name "MyVM" -MemoryStartupBytes 4GB -Generation 2 -Path "C:\VMS\" -VHDPath "C:\VMS\existing-disk.vhdx" -SwitchName "External Virtual Switch"
Create Virtual Machine with using a new differencing Virtual Hard Disk
In this example a new Generation 2 Virtual Machine will be created, under the name MyVM, with 4 GB of RAM, Configuration files will be placed under path C:\VMS\ to a new folder that has the same name as the VM, a new VHDX disk will be created and configured out of a parent disk and the VM’s NIC will be connected to the Virtual Switch called “External Virtual Switch”.
New-VHD -ParentPath "C:\VMS\Template_WS2022\Template.vhdx" -Path "C:\VMS\MyVM\MyVM_os_disk_1.vhdx" -Differencing; New-VM -Name "MyVM" -MemoryStartupBytes 4GB -Generation 2 -Path "C:\VMS\MyVM" -VHDPath "C:\VMS\MyVM\MyVM_os_disk_1.vhdx" -SwitchName "External Virtual Switch"
Create Generation 1 Virtual Machine
In this example a new Generation 1 Virtual Machine will be created, under the name MyVM, with 4 GB of RAM, Configuration files will be placed under path C:\VMS\ to a new folder that has the same name as the VM, a new VHDX disk sized at 60GB will be created under the same path and the VM’s NIC will be connected to the Virtual Switch called “External Virtual Switch”.
New-VM -Name "MyVM" -MemoryStartupBytes 4GB -Generation 1 -Path "C:\VMS\" -NewVHDPath "C:\VMS\MyVM\disk.vhdx" -NewVHDSizeBytes 60GB -SwitchName "External Virtual Switch"
Create Virtual Machine using a previous Configuration Version
In this example a new Configuration Version 8.0, Generation 2 Virtual Machine will be created, under the name MyVM, with 4 GB of RAM, Configuration files will be placed under path C:\VMS\ to a new folder that has the same name as the VM, a new VHDX disk sized at 60GB will be created under the same path and the VM’s NIC will be connected to the Virtual Switch called “External Virtual Switch”.
New-VM -Name "MyVM" -MemoryStartupBytes 4GB -Generation 2 -Version 8.0 -Path "C:\VMS\" -NewVHDPath "C:\VMS\MyVM\disk.vhdx" -NewVHDSizeBytes 60GB -SwitchName "External Virtual Switch"
Remove Virtual Machine
In this example, the VM named MyVM and its configuration file will be removed from Hyper-V. Note, when using Remove-VM cmdlet, Disks and Folders will not be removed automatically.
Remove-VM -Name "MyVM"
Remove Virtual Machine along with Configuration Files and Hard Disks
In this example, the VM named MyVM will be removed along with all related Folders, Confgiuration Files and Virtual Hard Disks.
$vm = get-vm "MyVM"; $vmpath = $vm.path; Remove-Item $vm.HardDrives.path -Force ; Remove-VM $vm -Force ; Remove-Item $vmpath -Recurse -Force
Configure Virtual Machine Processor Count
In this example, the VM named MyVM will be configured to use 2 Virtual Processors.
Set-VM -Name "MyVM" -ProcessorCount 2
Configure Virtual Machine Memory
In this example, the VM named MyVM will be configured to use 8GB of Ram Memory.
Set-VM -Name "MyVM" -MemoryStartupBytes 8GB
Rename Virtual Machine
In this example, the VM named MyVM will be renamed to MyNewVM.
Set-VM -Name "MyVM" -NewVMName "MyNewVM"
Configure Virtual Machine DVD Drive and ISO as Media
In this example, the VM named MyVM will be configured to use a Virtual DVD Drive and an ISO File as media.
Set-VMDvdDrive Name "MyVM" -ControllerNumber 1 -ControllerLocation 0 -Path "C:\ISO\Windowsserver2022.iso"
Configure Virtual Machine Boot Device
In this example, the VM named MyVM will be configured to use DVD boot from.
$vmDVDDrive = Get-VMDvdDrive -VMName "MyVM"; Set-VMFirmware -VMName "MyVM" -FirstBootDevice $vmDVDDrive
Configure Virtual Machine Network Interface to a Virtual Switch
In this example, the VM named MyVM will be configured to use the Virtual Switch “Default Switch”.
Get-VMNetworkAdapter -VMName "MyVM" | Connect-VMNetworkAdapter -SwitchName "Default Switch"
Move Virtual Machine Storage and Assosciated Files
In this example, the files and folders containing VM named MyVM will be moved to path “D:\MyVM”.
Move-VMStorage "MyVM" -DestinationStoragePath D:\MyVM
Export Virtual Machine
In this example, the VM named MyVM will be exported to path “C:\VMS\”.
Export-VM -Name "MyVM" -Path "C:\VMS\"
Checkpoint Virtual Machine
In this example, a new checkpoint will be created for VM named MyVM with a custom name.
Checkpoint-VM -Name "MyVM" -SnapshotName "Before applying .NET updates"
Remove Virtual Machine Checkpoint
In this example, all snapshots of VM named MyVM will be removed.
Get-VMSnapshot -VMName "MyVM" | Remove-VMSnapshot
Disconnect ISO File from Virtual Machine DVD Drive
In this example, the ISO media previously configured with VM named MyVM will be disconnected.
Get-VMDvdDrive -VMName "MyVM" | Set-VMDvdDrive -Path $null
Start Virtual Machine
In this example, the VM named MyVM will start.
Start-VM -VMName "MyVM"
Stop Virtual Machine
In this example, the VM named MyVM will be stopped.
### Gracefull Shutdown ###
Stop-VM -VMName "MyVM"
### Force Shutdown(Dirty) ###
Stop-VM -VMName "MyVM" -TurnOff
Restart Virtual Machine
In this example, the VM named MyVM will be restarted.
Restart-VM -VMName "MyVM"
Attach additional Virtual Hard Disk to Virtual Machine
In this example, an additional Virtual Hard Disk will be attached to the VM named MyVM.
Add-VMHardDiskDrive -VMName "MyVM" -Path "C:\VMS\MyVM\data_disk_02.vhdx"
Disconnect Virtual Hard Disk from a Virtual Machine
In this example, the Virtual Hard Disk with ID 2 on Controller, will be detached from the VM named MyVM.
Remove-VMHardDiskDrive -VMName "MyVM" -ControllerType "SCSI" -ControllerNumber 0 -ControllerLocation 2
Disable Automatic Virtual Machine Checkpoints (Windows 10/11 Hyper-V)
In this example, automatic Checkpoints will be disabled for the VM named MyVM.
Set-VM -VMName "MyVM" -AutomaticCheckpointsEnabled $False
Enable Virtual Machine Production Checkpoints
In this example, Production Checkpoints will be enabled for the VM named MyVM.
Set-VM -VMName "MyVM" -CheckpointType ProductionOnly
Enable Virtual Machine Nested Virtualization and Mac Address Spoofing
In this example, Nested Virtualization and Mac Address Spoofing will be enabled for the VM named MyVM.
Set-VMProcessor -VMName "MyVM" -ExposeVirtualizationExtensions $true; Set-VMNetworkAdapter -VMName "MyVM" -MacAddressSpoofing On
Resize Virtual Machine Hard Disk
In this example, the Virtual Hard disk of the VM named MyVM will be resized to 150GB.
$vm = get-vm "MyVM"; Resize-VHD -Path $vm.HardDrives.path -SizeBytes 150GB
Convert Virtual Machine Hard Disk to Fixed Size
In this example, the Virtual Hard disk of the VM named MyVM will be converted to Fixed.
$vm = get-vm "MyVM"; Convert-VHD -Path $vm.HardDrives.path -VHDType Fixed -DestinationPath $vm.Path\fixed.vhdx
Convert Virtual Machine Hard Disk from VHDX to VHD
In this example, the Virtual Hard disk of the VM named MyVM will be converted from VHDX to VHD.
$vm = get-vm "MyVM"; Convert-VHD -Path $vm.HardDrives.path -DestinationPath $vm.Path\MyVM_os_disk_1.vhd
Connect to Virtual Machine using PowerShell Direct
In this example, PowerShell Direct will be used to remotely connect to VM named MyVM.
Enter-PSSession -VMName "MyVM" -Credential (Get-Credential)
Display Virtual Machine Properties
In this example, properties of VM named MyVM will be displayed to an interactive table in a separate window.
Get-VM -VMName "MyVM" | Select * | Out-GridView
Conclusion
If you are reading this, it means that you have already gone through the entire list. PowerShell allows you to automate and reduce the time of repetitive tasks while enjoying Hyper-V to the fullest!
Thanks for reading my blog!
Feel free to drop your comment or question below.
[…] Click for Original Content Writer […]