Managing Hyper-V From the Command Line (Part 2)

If you would like to read the other parts in this article series please go to:

Introduction

In this article, you will learn how to access virtual machine lists and virtual machine summary information by using simple PowerShell commands. You will also learn the proper techniques for stopping and starting virtual machines from the command prompt.

If you worked through the first part of this article series then you know that installing the Hyper-V Management Library can be a lot of work. Hopefully with my guidance however, you were able to get the management library up and running without too much trouble. Now it is time to reap the rewards of working through the installation process by managing Hyper-V from PowerShell.

Before I Begin

Before I get started, I need to point out that each time you open PowerShell you will have to import the Hyper-V Management Library before you will be able to use any of the commands that I am about to show you. As you may recall, the command for importing the management library is:

Import-Module “C:\Program Files\Modules\Hyperv\Hyperv.psd1”

Basic Virtual Machine Management

As you would probably expect, the Hyper-V Management Library supports a huge number of commands. As such, I want to start out by showing you a few basic things that you can do from PowerShell.

Unless you happen to have the names of all of your virtual machines memorized, most of the Hyper-V related operations that you can perform from PowerShell hinge on the ability to retrieve a list of your virtual machines. If you want to see which virtual machines exist on your Hyper-V server, use the following command:

Get-VM

The Get-VM command will display a table showing each virtual machine and the name of the host server on which it currently exists (this is especially handy for large clusters in which a virtual machine could be running on any one of several cluster nodes). The Get-VM command also shows you the current state of each virtual machine as well as how long each virtual machine has been up and running. The table also contains an owner field, but that field is not typically populated.

Now that you know how to retrieve a list of the virtual machines that exist on your server, imagine that you wanted to see some extra information about a particular virtual machine. You could do so by using the Get-VMSummary command, along with the name of the virtual machine that you want to examine.

Figure A shows both the Get-VM command and the Get-VMSummary command in action. As you can see in the figure, the Get-VMSummary command shows you the virtual machine’s name, IP address, operating system, and several other statistics.


Figure A: The Get-VMSummary command provides detailed information about a virtual machine.

Incidentally, there are a couple of nice variations on these commands. If for example, you want to retrieve a list of the virtual machines from a specific server you can append the –Server parameter to the Get-VM cmdlet. For example, to see all of the virtual machines on a server named Main you would use the following command:

Get-VM –Server Main

Similarly you can retrieve summary information from a virtual machine on a remote server by appending the –Server switch. For example, if you wanted to see information about a virtual machine named DNS on a server named Main you would use this command:

Get-VMSummary DNS –Server Main

You can see how these commands work in Figure B.


Figure B: The Hyper-V Management Library can be run against remote servers even if the management library is not installed on the remote server.

Of course the Hyper-V Management Library is useful for much more than just gathering information about your virtual machines. It is also possible to start and stop virtual machines from the command line. The command that you would use to start a virtual machine is: Start-VM <virtual machine name>

When you issue this command, you will see a rather ominous warning message telling you that a job is running in the background, as shown in Figure C. You can ignore this message, as it merely means that the virtual machine is starting and that a WMI job initiating the start action has been queued.


Figure C: You can ignore this warning message.

Actually, if you read the warning shown above carefully, you will notice that PowerShell actually gives you a WMI job identification number as well as the cmdlets that you would use to check the status of the job. While writing this article, I entered these commands several times and in my opinion using these commands isn’t very practical because the commands involve a lot of very tedious typing. It is a lot faster to just use the Get-VM command to verify that the virtual machine started, as shown in Figure D. Notice in the figure that I specified the VM name when I used the Get-VM command so that I didn’t have to sort through the full list of virtual machines.


Figure D: You can use the Get-VM command to check a virtual machine’s status.

Being that the command used to start a virtual machine is Start-VM, it probably stands to reason that the command used to stop a virtual machine is Stop-VM. While Stop-VM is a perfectly valid command, there is something important that you need to know before you use it.

The Stop-VM command essentially just puts the virtual machine into a stopped state. This command is more or less the virtual server equivalent to yanking the power cord out of the back of a machine. Although the Stop-VM command might sometimes be necessary, it is better to shut down virtual machines by using the Invoke-VMShutdown command. This command actually tells the virtual machine’s operating system to perform a graceful shutdown. Of course it goes without saying that this command was designed for use with virtual machines that are running Windows operating systems.

If you look at the screen capture shown in Figure E, you can see that I first entered the Stop-VM command. I did this only to demonstrate that Stop-VM is a valid command. When PowerShell prompted me as to whether or not I really wanted to stop the virtual machine I aborted the process by pressing N. I then used the Invoke-VMShutdown command (along with the virtual machine name) to start a graceful shutdown process. Once again, PowerShell asked me if I really wanted to perform the operation. This time I entered Y and the shutdown was initiated. Finally, I verified that the virtual machine was shut down by entering the Get-VM command.


Figure E: Whenever possible you should use the Invoke-VMShutdown command rather than the Stop-VM command.

Conclusion

Now that you know how to examine a virtual machine summary and how to start and stop virtual machines from the command line, it is time to move on to some more exciting things. In Part 3, I am going to start out by showing you two troubleshooting commands – PingVM and TestVmHeartBeat. Next, I am going to show you some tricks for adjusting virtual machine memory allocations.

If you would like to read the other parts in this article series please go to:

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top