Weekend Scripter: Use PowerShell to Create a New Virtual Machine

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to easily create a new virtual machine on a computer running Windows 8.

Playing around with the Hyper-V module

Microsoft Scripting Guy, Ed Wilson, is here. It is autumn in Charlotte, North Carolina in the United States—as autumn as we get in the Deep South. The Scripting Wife and I are heading to Raleigh, North Carolina for the North Carolina State Fair (they have a horse show), and we will be speaking at the Tri-Pass SQL Server User Group while we happen to be in the area. The Tri-Pass SQL Server User Group is a very active group, and it does an exceptionally great job with the community. In the past, the Scripting Wife and I have had the opportunity to speak at a SQL Server Saturday hosted by the group in addition to the user group meetings. We have been looking forward to this meeting because it offers the chance for us to meet some of our friends who we have not seen for awhile.

In addition, we are now exactly two weeks away from Windows PowerShell Saturday #003 in Atlanta, Georgia. This event takes place on October 27, 2012, and there are a few tickets remaining for the event. The lineup of speakers is very strong (I am making two presentations), and we are expecting to see people attending from more than a half dozen states. It will be a blue-ribbon event, and we hope to see you there.

Because I recently paved (formatted and reinstalled) my laptop with Windows 8 (it was running a beta build of Windows 8, and I did not want to upgrade to the RTM bits until after Windows PowerShell Saturday 002 in Charlotte finished), I am thinking that it is a good time to play around with the Hyper-V module in Windows 8. This is because I need to create a number of virtual machines to support Windows PowerShell Saturday 003 in Atlanta and to support my talk for the Tri-Pass SQL Server User Group.

Create a new virtual machine

The first thing to do is to import the Hyper-V module. Unfortunately, there is confusion as to whether it should be HyperV or Hyper-V—even among Microsoft writers. Very often, it is a good idea to hedge one’s bets as to how things will turn out when looking for the material. With the Import-Module cmdlet, this means I can use wildcard characters. To import the Hyper-V module (this is the actual name, by the way—it DOES have a hyphen), I use hyper*. The following command (ipmo is an alias for Import-Module) imports the Hyper-V module.

ipmo hyper*

After I import the Hyper-V module, I use the Get-Command cmdlet to see what cmdlets exist. Now a completely different problem rears its head. There are 164 cmdlets exposed by the Hyper-V module; so I am bewildered with a huge number of cmdlets. To put this in perspective, Windows PowerShell 1.0 shipped with 129 cmdlets, so we have an huge number of cmdlets with which to deal.

PS C:\> gcm -Module hyper* -CommandType cmdlet | measure


Count    : 164

Average  :

Sum      :

Maximum  :

Minimum  :

Property :

Not only are there 164 cmdlets, but I do not have Help for the Hyper-V module on my laptop. DUDE (or DUDETTE)!

Oh wait—I am a Scripting Guy. I don’t need any Help files. I can use Get-Command and even Get-Help to figure out what I need. Because I want to create a new virtual machine, I know the cmdlet will start with the verb New. A quick Get-Command command and I have weeded out 157 cmdlets that I do not need to look at. Here is the result.

PS C:\> gcm -Module hyper* -Verb new

CommandType     Name                                               ModuleName

———–     —-                                               ———-

Cmdlet          New-VFD                                            Hyper-V

Cmdlet          New-VHD                                            Hyper-V

Cmdlet          New-VM                                             Hyper-V

Cmdlet          New-VMReplicationAuthorizationEntry                Hyper-V

Cmdlet          New-VMResourcePool                                 Hyper-V

Cmdlet          New-VMSan                                          Hyper-V

Cmdlet          New-VMSwitch                                       Hyper-V

It does not take a Scripting Guy to see that to create a new virtual machine; I probably need to investigate the New-VM cmdlet. I use the Get-Help cmdlet and display Help on the New-VM cmdlet. There are three different command sets.

Ah wait, I said that my laptop had no Help for the Hyper-V module. Yes, I did, but the Help subsystem examines the metadata from cmdlets and displays the syntax even if Help does not exist. The image shown here illustrates this technique.

Image of command output

Without cmdlet Help, I am not certain which are required or optional parameters. There is one way to find out—type a command. I use the following command (this is a single logical command that wraps to two lines in Microsoft Word).

new-vm c7 -MemoryStartupBytes 512 -SwitchName InternalSwitch -BootDevice cd -NewVHDPath “c:\vms\c7\Virtual Hard Disks\c7.vhdx” -NewVHDSizeBytes 127

The following friendly error message appears. The error message states that the minimum amount of memory that I can specify is 8 MB. I thought I gave the virtual machine 512 megabytes of memory, but evidently, I did not.

Image of error message

I notice that the error message specifies MB, and I decide to try the command a second time by using the standard admin constants that Windows PowerShell uses. The revised command is shown here. (Again, keep in mind that this is a single logical command. I have not added any line wrapping or line continuation.)

new-vm c7 -MemoryStartupBytes 512MB -SwitchName InternalSwitch -BootDevice cd

 -NewVHDPath “c:\vms\c7\Virtual Hard Disks\c7.vhdx” -NewVHDSizeBytes 127GB

This time, a progress bar displays briefly, and information about the new virtual machine appears in the Windows PowerShell console. The output is shown here.

Image of command output

I can now edit information for my virtual machine in Hyper-V Manager or with the Hyper-V cmdlets, but at least I have a new virtual machine. This did not save a whole lot of time if all I wanted to do was create a single virtual machine; but if I needed to create dozens of virtual machines, the command would save a lot of time.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Discussion is closed.

Feedback usabilla icon