Microsoft Hyper-V: Guide To Common Commands
Manage your Microsoft Hyper-V virtual environment in the most efficient and productive manner using these useful commands.
May 12, 2015
Image: Microsoft TechNet
PowerShell console
The latest version of Windows PowerShell, PowerShell 3.0, is installed by default in Windows Server 2012. PowerShell is basically a task-based command-line and scripting language designed for system administrators. Built on the .NET framework, it helps power users and professionals control and automate administration of the Windows OS and all related applications. This feature can be enabled or disabled from Server Manager.
Windows Server offers different ways to access the Windows PowerShell console. Users can access Windows PowerShell through:
The shortcut on the Taskbar
The Start screen
In Server Manager, users can go to Tools and choose Windows PowerShell
"PowerShell" command in Command Prompt
To access Windows PowerShell with Administrator permissions, right-click the Windows PowerShell app, and in the app bar, click Run as administrator.
Basic commands for virtual machine management
Get-VM
This command gives users a list of all virtual machines across multiple host servers. Users can filter the VM list for specific host servers. Additionally, Get-VM command allows users to export the VM list into CSV format. Here are some examples:
PS C:\> Get-VM
PS C:\> Get-VM ComputerName Server1
PS C:\> Get-VM ComputerName Server1 | EXPORT-CSV C:\Powershell\VMlist.csv
Get-VM Summary
This command gives users detailed information about any virtual machine. This command results in information such as CPU counts, number of snapshots, guest OS, uptime, hostname, IP address, FQDN, CPU load, memory usage, and more. Following is the syntax:
PS C:\> Get-VMSummary VM01
PS C:\> Get-VMSummary VM03 ComputerName Server1
New-VM
The New-VM command creates a new virtual machine. This command allows users to define additional parameters to set VM configuration such as VM generation, startup memory, boot device and VHD path (for both new and existing VHD files).
PS C:\> New-VM Name "VM04" MemoryStartupBytes 512MB
PS C:\> New-VM Name "VM05" MemoryStartupBytes 1GB VHDPath d:\vhd\BaseImage.vhdx
PS C:\> New-VM Name "new 2" MemoryStartupBytes 1GB NewVHDPath d:\vhd\base.vhdx
Start- and Stop-VM
The Start-VM command allows users to start a virtual machine. Users either can start a single VM or can define more parameters to run this command for starting multiple virtual machines.
PS C:\> Start-VM Name VM01
PS C:\> Start-VM VM0* -asjob
Similarly, the Stop-VM command allows users to stop single or multiple virtual machines simultaneously.
PS C:\> Stop-VM VM0*
PS C:\> Stop-VM VM01 turnoff
PS C:\> Stop-VM VM01 save force
Image: Microsoft TechNet
Image: Microsoft TechNet
Managing Checkpoints/Snapshots
Microsoft's "virtual machine snapshots" were renamed as "virtual machine checkpoints" for the Windows Server 2012 R2 version.
Get-VM Snapshot
This command shows users a list of all current snapshots across different host machines: PS C:\> Get-VMSnapshot VMName VM03
PS C:\> Get-VM Name VM03 | Get-VMSnapshot SnapshotType Standard
PS C:\> Get-VM Name VM03 | Get-VMSnapshot SnapshotType Standard
Create VM Checkpoint
This command allows users to create a new checkpoint for any virtual machine. Parameters for host servers can be added to specify the VM.
PS C:\> Checkpoint-VM -Name Test -SnapshotName BeforeInstallingUpdates
PS C:\> Get-VM Test ComputerName Server1 | Checkpoint-VM
Remove Checkpoint
The Remove-VMSnapshot command deletes a virtual machine checkpoint. Additional parameters can be added to filter a snapshot with any specific name. It also allows users to define conditional parameters, such as deleting any snapshot older than 90 days.
PS C:\> Get-VM TestVM | Remove-VMSnapshot
PS C:\> Get-VM TestVM | Remove-VMSnapshot Name Experiment*
PS C:\> Get-VMSnapshot -VMName TestVM | Where-Object {$_.CreationTime -lt (Get-Date).AddDays(-90) } | Remove-VMSnapshot
Restoring a snapshot
The Restore command allows users to restore any snapshot to the specified virtual machine.
PS C:\> Restore-VMSnapshot Name Test VMName TestVM
Image: Microsoft TechNet
Import and Export VM
Import VM
The Import-VM command imports a virtual machine from a file. The command imports a virtual machine from its configuration file, and it also allows users to import a VM with a new unique identifier (not the one in the configuration file). This is useful when importing multiple copies of a virtual machine, since each virtual machine must have a unique identifier.
PS C:\> Import-VM Path 'D:\Test\VirtualMachines\5AE40946-3A98-428E-8C83-081A3C6BD18C.XML'
PS C:\> Import-VM -Path 'D:\Test2\Virtual Machines\8F148B6D-C674-413E-9FCC-4FBED185C52D.XML' Copy GenerateNewId
Export VM
The Export VM command helps users to export virtual machines from current hosts to other host servers. This export process includes exporting virtual machine configuration, a copy of the VHD or VHDX file, and snapshots (if any).
Additional parameters can be added to export multiple virtual machines with a single command.
PS C:\> Export-VM Name Test Path D:\
PS C:\> Get-VM ComputerName Server1 | Export-VM Path D:\
Image: Microsoft TechNet
Manage VHDs
Get-VHD
The Get-VHD command helps users get a list of all virtual hard disk objects associated with any virtual hard disk.
PS C:\> Get-VHD Path c:\test\testvhdx.vhdx
New VHD
This command allows users to create a new virtual hard disk. The command also allows users to create a VHDX-format differencing virtual hard disk. Configuration of fixed or dynamic hard disks can also be defined using additional parameters.
PS C:\> New-VHD Path c:\Base.vhdx SizeBytes 10GB
PS C:\> New-VHD ParentPath c:\Base.vhdx Path c:\Diff.vhdx Differencing
PS C:\> New-VHD -Path C:\fixed.vhd Fixed SourceDisk 2 SizeBytes 1TB
Merge-VHD
In a differencing virtual hard disk chain, the Merge-VHD command merges virtual hard disks. The merge is from a specified source child disk to a specified destination child disk.
The merge is an offline operation; the virtual hard disk chain must not be attached when the merge is initiated.
PS C:\> Merge-VHD Path c:\test\Child4.vhdx DestinationPath c:\test\Child2.vhdx
Mount- and Dismount-VHD
These commands will mount or dismount the virtual hard disks to any virtual machine. When mounting any VHD the command allows users to state read-only clearances when required.
PS C:\> Mount-VHD Path c:\test\testvhdx.vhdx
PS C:\> Mount-VHD Path c:\test\testvhdx.vhdx ReadOnly
PS C:\> Dismount-VHD Path c:\test\testvhdx.vhdx
Creating additional VM resources
Increase vCPU
Once a number of virtual processes have been added to any virtual machine, VM creation can be changed using the PowerShell command on Hyper-V. The Set-VMCPUCount command allows users to modify the current vCPU count.
PS C:\> Get-VMCPUCount "VM01"
PS C:\> Set-VMCPUCount "VM01" 2
Increase RAM
Similar to the above Set-VMCPUCount command, the Set-VMMemory command also allows users to modify the current memory assigned to any virtual machine. This command also allows users to configure RAM as dynamic or fixed memory.
PS C:\> Get-VMMemory TestVM
PS C:\> Set-VMMemory VM "TestVM" Memory 2.0GB Server Hyper-V
PS C:\> Set-VMMemory TestVM -DynamicMemoryEnabled $true -MinimumBytes 64MB -StartupBytes 256MB -MaximumBytes 2GB
Add new VHD to any VM
This command allows users to assign a new VHD to any specific VM.
NOTE: This command will not create a new VHD, but this command allows users to assign any VHD to the VM.
PS C:\> Add-VMHardDiskDrive -VMName Test -Path D:\VHDs\disk1.vhdx
Add new network adaptor
The Add-VMNetworkAdapter command adds a virtual network adapter to a virtual machine; users can also connect it to a virtual switch named Network.
PS C:\> Add-VMNetworkAdapter VMName Redmond Name "Redmond NIC1"
Attach SCSi controller
The Add-VMScsiController command adds a SCSI controller to a virtual machine.
PS C:\> Add-VMScsiController VMName TestVM
Image: Microsoft TechNet
Troubleshooting commands
Ping-VM
This command is very helpful when it is required to check the communication of a virtual machine. The command uses a virtual machine's integration services to discover the Fully Qualified Domain Name (FQDN), and then performs a regular Internet Control Message Protocol (ICMP) ping.
PS C:\> Ping-VM TestVM Server Hyper-V
Testing Virtual Machine Heartbeats
This command helps in testing the heartbeat of a virtual machine. Like ping, this command also uses the virtual machine's integration services.
PS C:\> Test-VmHeartBeat TestVM-Timeout 300
Test-VHD
The Test-VHD command allows users to perform validation of any virtual hard disk. This command should detect if there is a potential problem with a VHD, such as a broken differencing chain.
PS C:\> Test-VHD Path VHD01.vhdx
Test-VM network adaptor
The Test-VMNetworkAdapter command tests connectivity between virtual machines by using ICMP ping packets. Ping verifies IP-level connectivity to another TCP/IP computer by sending ICMP Echo Request messages.
PS C:\> Test-VMNetworkAdapter VMName "ContosoVM01" Receiver SenderIPAddress "10.20.20.5" ReceiverIPAddress "10.20.20.6" VMNetworkAdapterName "ContosoNic01"
Image: Microsoft TechNet
Additional commands
Creating a report
This command allows users to store the output of any command into a CSV or HTML file. This command helps to save query results for future purposes.
PS C:\> Get-VMMemory | ConvertTo-HTML | Out-File C:\temp\Test.HTM
PS C:\> Get-VMMemory | ConvertTo-CSV | Out-File C:\temp\Test.csv
Comparing VM files
The Compare-VM command returns a compatibility report when it compares a virtual machine host to a virtual machine for compatibility. This command is particularly useful for migrating or importing a virtual machine that is incompatible with the aimed Hyper-V server.
PS C:\> Compare-VM Name TestVM DestinationHost TestDestinationHost
PS C:\> $report = Compare-VM -Path 'D:\TestVM\Virtual Machines\53EAE599-4D3B-4923-B173-6AEA29CB7F42.XML'
Copy-VMFile
The Copy-VMFile command copies a file to a virtual machine.
PS C:\> Copy-VMFile TestVM -SourcePath "D:\Test.txt" -DestinationPath "C:\Temp\Test.txt" -CreateFullPath -FileSource Host
Measure-VM
The processor usage, memory usage, network traffic, and disk capacity for one or more virtual machines receive data reports from the Measure-VM command.
NOTE: The Measure-VM command makes the data available for reporting when the resource metering is enabled for a virtual machine.
PS C:\> Measure-VM VMName TestVM
Conclusion
Windows PowerShell allows administrators to manage Hyper-V in a more effective way than GUI. Administrators can perform additional operations through PowerShell, and the most commonly used commands for Hyper-V include:
Basic commands that can help administrators manage basic VM operations such as Get VM list, VM summary, create a new VM or start/stop VM
Commands for managing checkpoints, including getting a list of current checkpoints, as well as create, remove and restore checkpoints.
Import and Export VM commands simplify administrative efforts in moving a VM from one host to another.
Commands like get a list of VHDs, create a new VHD, mount/dismount VHD and merge VHDs help in VHD management.
Some commands can help in configuring additional VM resources, including increasing vCPU and RAM, adding new VHD, adding a new network adaptor and attaching SCSi controller.
Hyper-V also helps administrators through troubleshooting commands like ping VM, test VM heartbeat, test VHD, test VM network adaptor and more.
Additional commands simplify the administration of creating reports, comparing VMs, copying VMs and measuring VMs.
These commands can be very handy for an administrator managing a virtual environment and can contribute to the most efficient and productive performance of daily tasks.
Conclusion
Windows PowerShell allows administrators to manage Hyper-V in a more effective way than GUI. Administrators can perform additional operations through PowerShell, and the most commonly used commands for Hyper-V include:
Basic commands that can help administrators manage basic VM operations such as Get VM list, VM summary, create a new VM or start/stop VM
Commands for managing checkpoints, including getting a list of current checkpoints, as well as create, remove and restore checkpoints.
Import and Export VM commands simplify administrative efforts in moving a VM from one host to another.
Commands like get a list of VHDs, create a new VHD, mount/dismount VHD and merge VHDs help in VHD management.
Some commands can help in configuring additional VM resources, including increasing vCPU and RAM, adding new VHD, adding a new network adaptor and attaching SCSi controller.
Hyper-V also helps administrators through troubleshooting commands like ping VM, test VM heartbeat, test VHD, test VM network adaptor and more.
Additional commands simplify the administration of creating reports, comparing VMs, copying VMs and measuring VMs.
These commands can be very handy for an administrator managing a virtual environment and can contribute to the most efficient and productive performance of daily tasks.
Image: Microsoft TechNet
About the Author(s)
You May Also Like