Discover how Windows PowerShell and the built-in PowerShell cmdlets that ship with Hyper-V can help automate a variety of server admin tasks on Hyper-V virtual machines.
Performing a set of common tasks manually in Windows may take a considerable amount of time and can quickly become taxing when the task is a repetitive necessity. For example, if you need to set the memory for ten virtual machines running on Hyper-V, you will need to use the Hyper-V Manager GUI and then edit the settings for all ten virtual machines.
Fortunately, Microsoft has to put a lot of effort into designing a common framework called PowerShell for scripting Windows components in an easy, straightforward manner.
Windows PowerShell is as robust as unix shell and provides commands or cmdlets for almost all roles and features available on operating systems running Windows Server 2008 and later. With Windows PowerShell you can reduce the time and effort it takes to perform the same set of tasks such as the one illustrated above.
Hyper-V ships with 164 built-in PowerShell cmdlets. These cmdlets range from handling very simple, basic tasks to advanced ones such as live migrations of virtual machines across Hyper-V servers. To get the list of all the available Hyper-V cmdlets, you can use the following command in PowerShell:
Get-Command -Module Hyper-V
This command enumerates the list of Hyper-V cmdlets for both Hyper-V Server and any virtual machines and then shows in the command window as shown in the below screenshot:
The PowerShell cmdlets are also user friendly. Just by looking at the name you can get a sense of what it is all about or you can at least get some idea about the command's functionality. For example, Add-VMNetworkAdapter, as shown in the above screenshot, is used to add a Vritual Machine Network Adaptor.
So if you are looking for a specific cmdlet out of 164 cmdlets, you can type the below command. Let's say you want to list all the cmdlets that have the word "Add" in it:
Get-Command -Module Hyper-V -Name *Add*
The command lists all cmdlets that have "Add" as shown in the below screenshot:
Once you have found the cmdlet, you can use it to perform the tasks of your choice. If you are still unsure what a cmdlet does, you can always use "
Get-Help <Hyper-V cmdlet>" cmdlet to get help and examples on the cmdlet.
There are three switches available to be used with "Get-Help" cmdlet: "-Examples", "-Detailed" and "-Full."
To get help and examples on a specific Hyper-V cmdlet called AddVMNetworkAdapter, you can type the following commands:
Get-Help Add-VMNetworkAdapter -Examples
Get-Help Add-VMNetworkAdapter -Detailed
Get-Help Add-VMNetworkAdapter -Full
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:
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.
Follow ServerWatch on Twitter and on Facebook