Hyper-V PowerShell Commands

I just spent the evening working on a HyperV server, from the command line. I realized that I don't have a nice, one-place lookup for the comman commands I've been using. They are spread over a number of websites in my favourites bar. So, this is my one-stop reference for Hyper-V Powershell Commands. (Credit at the bottom)


Virtual Machines

New virtual machine

New-VM -Name Testmachine -path C:\vm-machine -MemoryStartupBytes 1024MB

New virtual disk

New-VHD -Path c:\vm-Machine\Testmahcine\Testmachine.vhdx -SizeBytes 60GB -Dynamic

Attach The Disk

Add-VMHardDiskDrive -VMName <name> -Path <path>

Add an ISO image to the Virtual Optical Drive

Set-VMDvdDrive -VMName <name> -ControllerNumber 1 -Path <path>

Add a new Network Adapter to the VM

Add-VMNetworkAdapter -VMName <name> -Name “Prod NIC” [-IsLegacy $true ]

Connect-VMNetworkAdapter -VMName <name> -SwitchName ‘Private Switch’

Set the Start policy for a VM

Set-VM -Name <name> -AutoStartAction Start

Virtual Machine Controls

Show all the VMs on the host

Get-VM

Starting it up:

Start-VM -Name <name>

Stopping the VM

Stop-VM -Name <name>

Take a snapshot of the VM

Checkpoint-VM -Name ProdServer* -SnapshotName PreMigrationSnapshot

Make a VM from a snapshot

Export-VMSnapshot -Name ‘PosUpdates’ -VMName NewVM -Path H:\NewVMfromSnapshot

 

Host Management

Add a virtual switch for the VMs

New-VMSwitch “External Switch” –NetAdapterName “Ethernet Connection” –AllowManagementOS $true

Get the RAM of the Host Hyper-V Server

(systeminfo | Select-String 'Total Physical Memory:').ToString().Split(':')[1].Trim()

Credit:

  • http://stackoverflow.com/questions/17681234/how-do-i-get-total-physical-memory-size-using-powershell-without-wmi
  • http://www.thegeekstuff.com/2014/02/create-vm-from-powershell/
  • http://andersonpatricio.ca/performing-daily-task-with-powershell-to-manage-hyper-v-server/