Cmdlet for Creating a Virtual Machine on Hyper-V Page 2
Cmdlet for Creating a Virtual Machine on Hyper-V
If you need to create a Virtual Machine on Hyper-V, you can use the New-VM cmdlet as listed below:
New-VM -Name MyVM -MemoryStartupBytes 1024MB -path C:\MyAllVMs
The above command creates a new virtual machine on Hyper-V with memory set to 1 GB (1024MB). Virtual machine configuration files and VHD files are stored in the C:\MyAllVMs folder as specified in the above command using the -path switch.
It is difficult to remember all the switches available with a PowerShell cmdlet. Of course, you can use the "Get-Help" cmdlet to get more help on the cmdlet, but there is another way to look at the same information in the PowerShell ISE GUI. To see the detailed help, type "Show-Command" in the PowerShell window. Typing "Show-Command" brings the GUI as shown in the below screenshot.
In "Modules", select "Hyper-V" to filter all the Hyper-V cmdlets. Next type the name of the Hyper-V module to see the parameters/switches associated with it as shown in the above screenshot. If you are unsure of whether a cmdlet will work or not, you can always use the "WhatIf" parameter as shown in the red circle in the above image to actually test the command or get its result beforehand.
The "WhatIf" parameter is always added at the end of the cmdlet command. For example, to see if a new virtual machine will be created or not, you can test it before actually creating the virtual machine as shown in below screenshot:
To manage virtual machines and their components every day, you can use the following PowerShell Hyper-V cmdlets examples. These cmdlets are very useful when performing common daily tasks such as modifying virtual machine network settings, adding virtual hard disks, exporting/importing virtual machines and so on for one or more Virtual Machines.
Tip: You can always add an asterisk (*) after the Virtual Machine name to let the cmdlet take action on all Virtual Machines starting with that name. For example, you can stop all Virtual Machines that start with the word "Exchange" as shown in the below example:
Stop-VM -Name Exchange*
If you need to export a Virtual Machine, use the Export-VM command as listed below:
Export-VM -Name VM1 -Path C:\Temp
To export all Virtual Machines on a Hyper-V (which is required if you are migrating all Virtual Machines from one Hyper-V Server to another Hyper-V Server), you can use the below command with the Get-VM cmdlet:
Get-VM | Export-VM -Path C:\AllVMs
The above command will export all the virtual machines including virtual machine configuration files and VHD files to the C:\AllVMs folder.
To move the Virtual Machine storage from one location to another, use the Move-VMStorage as shown in below example:
Move-VMStorage VM1 -DestinationStoragePath E:\AllVMs
The "Set-VM" cmdlet allows you to change the configuration of the Virtual Machine as shown in the below example:
Set-VM -Name VM1 -DynamicMemory -MemoryMinimumBytes 2GB -MemoryMaximumBytes 4GB -MemoryStartupBytes 1GB
To stop and start a Virtual Machine:
Start-VM -Name VM1
Stop-VM -Name VM1
To create snapshots of Virtual Machines, you can use the "Checkpoint-VM" cmdlet as detailed in the below example:
Checkpoint-VM -Name VM1 -SnapshotName BeforeChanges
There are a number of PowerShell cmdlets available for Hyper-V Server and virtual machines. You just need to search for the right cmdlet to help you accomplish your task.
In this article we learned how Windows PowerShell is closely integrated with Windows components such as Hyper-V role and how much easier PowerShell makes administrators' lives when it comes to performing daily maintaince tasks on Hyper-V virtual machines.
We also learned a new way to explore the help function for Hyper-V PowerShell cmdlets. The article also explained some of the common examples that are useful for the day-to-day administration of virtual machines running on a Hyper-V server.
Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He has specialized in Microsoft Technologies since 1994 and has followed the progression of Microsoft Operating System and software. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Solution IDs for www.Dynamic-SpotAction.com. Nirmal can be reached at firstname.lastname@example.org.
Read more on "Server OS Spotlight" »