Hyper-V Tips
Show all Hyper-V powershell cmdletsGet-Command -Module Hyper-V
Show information about the VM-HOST
Get-VMHost
Show detailed information about the host:
Get-VMhost | Format-List
Show information about VM Guests
Get-VM
More detailed info:
Get-Vm | Format-List
You can drill down and view additional information on individual properties:
Get-VM -vmname hostname | select -ExpandProperty NetworkAdapters
Backing up Hyper-V Virtual Machines
By default Hyper-V VM Virtual Hard Disks (.vhdx) are stored in the below location:
C:\Users\Public\Documents\Hyper-V
You can backup a VM by simply copying this .vhdx file when the machine is not running. Note, snapshots and settings will not be included. To restore the VM open Hyper-V and select 'new virtual machine'. You can then browse for your backed up .vhdx file.
In Server 2012 R2 you can export a VM (while it is even running!), including checkpoints and settings, by using the below powershell commands:
Export all VMs to D:\VM_Backup:
Get-VM | Export-VM -Path D:\VM_Backup
Export a VM named server1:
Export-VM -Name server1 -Path D:\VM_Backup
To restore the VM open Hyper-V and select 'import virtual machine'. You can then browse for the folder you exported your VM too. All snapshots and settings will also be imported.
Working with snapshots
Show snapshots:
Get-VMSnapshot -VMName server1
Taking a snapshot:
get-vm -VMName server1 | Checkpoint-VM -SnapshotName PS_Snap
Taking a snapshot for multiple VMs
$VMNames | GET-VM | CHECKPOINT-VM -SnapshotName 'same_snapshot_name_for_all'
View snapshots on a VM:
Get-VM -VMName vmsftp | Get-VMSnapshot
Restoring a snapshot:
Get-VM -VMName hostname | get-vmsnapshot -name 'particular_snap' | restore-vmsnapshot
Restoring snapshots with the same name for multiple VMs.
GET-VM | GET-VMSnapshot -Name 'same_snapshot_name_4_all' | RESTORE-VMSnapshot
Removing a snapshot:
Get-VMSnapshot -VMName server1 | Remove-VMSnapshot
The below script can be run as a scheduled task to take a weekly snapshot of a guest VM:
import-module hyper-v
$dated = get-date -f ddMMyyyy
$snapshot = $dated + 'weekly_snapshot'
GET-VM -vmname hostname | CHECKPOINT-VM -SnapshotName $snapshot
Starting a VM
Start-VM -VMName hostname
The below script can be run under a startup scheduled task to boot up specific guest VMs:
import-module hyper-v
Get-VM -VMName hostname, hostname2, hostname3 | Start-VM
Remove a VM
Remove-VM -VMName hostname
Restart a VM
Note this a hard reset (unsafe).
Restart-VM -VMName hostname
Saving (pausing) a VM and how to resume it
Save a VM:
suspend-vm -vmname hostname
Resume a saved VM:
resume-vm -vmname hostname
Stopping a VM
Shutdown a VM:
Stop-VM -VMName server1
Force a shutdown:
Stop-VM -VMName server1 -force
Turn off the power:
Stop-VM -VMName server1 -turnoff