Create a New Virtual Machine with PowerShell: Part 1

Create a New Virtual Machine with PowerShell: Part 1

  • Comments 4
  • Likes

Summary: Microsoft Windows PowerShell MVP and honorary Scripting Guy, Sean Kearney, begins a three-part series about creating new virtual machines.

Microsoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to the blog, I welcome you, and I encourage you to catch up with Sean’s previous blogs.

Sean is a Windows PowerShell MVP and an Honorary Scripting Guy. Sean’s session at TechEd NA and TechEd Europe this year is Integrating with Microsoft System Center 2012 and Windows PowerShell. In his free time, Sean has written several blog posts about Hyper-V and some other cool stuff, and for the next few weeks, Sean will be the designated guest blogger on Fridays.

Take it away Sean…

I felt a little poke on my shoulder the other day. A friend came and asked me how hard it is to create a virtual machine with Windows Powershell and Hyper-V.

I typed in the following cmdlet and walked off.

NEW-VM

Image of command output

He blinked and came back to me. “OK, smart guy. I see. That created a new BLANK virtual machine called “New Virtual Machine” with 512 MB of RAM, no hard drive, and unattached to any LAN. Being a little funny today, aren’t we?”

I was feeling a bit mischievous at the moment. I’m allowed from time-to-time. “Well, I did meet your needs. You said ‘create a virtual machine.’ So how about we define our parameters?”

“OK. I would like to see an example of creating a virtual machine with the following configuration:”

Name of “Brad VM SCSM”

2 gigabytes of RAM

60 gigabyte hard drive

2 CPU cores assigned

Connected to my virtual network called “Hyper-V-Prod1”

I smiled and looked across to my friend. “No problem. So, from what I’ve been showing you about Windows PowerShell, how would you determine the use of this cmdlet?”

He looked over. “First off, I remember that using Get-Help should show me the available parameters for the cmdlet.”

GET-HELP NEW-VM

Image of command output

“Then if I need to find examples of the cmdlet in use, I would run the same with the –examples parameter.”

GET-HELP NEW-VM -examples

Image of command output

Looking at the information before us, we see that we can at least create a virtual machine with 2 GB of RAM, based on the first example, by using this command:

NEW-VM –Name “Brad VM SCSM” –MemoryStartupBytes 2048

Brad looked up. “Now that should work, but we still need the virtual hard disk in this configuration. From what I see in the second example, we could specify a virtual hard disk if we knew the physical path to place it. I know my VHDs are sitting on Drive E:\VHD on my Hyper-V host in the lab. So I should just be able to pick a VHD file name that is unique to my virtual machine.”

NEW-VM –Name “Brad VM SCSM” –MemoryStartupBytes 2048 –NewVHDPath E:\VHD\BradVMSCSM-C.VHDX

But when he ran the cmdlet, he got the following prompt:

NewVHDSizeBytes:

Scratching his head, he looked over. “What happened?”

I smiled. “It was a poorly written example, apparently. Human error. Let’s grab lunch first, and then we’ll take a look back.”

~Sean

Thanks, Sean. This is great, and I cannot wait to see the next two posts. I invite everyone to join me tomorrow for more cool Windows PowerShell stuff.

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 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • You left this hanging until next week, so I'll give it a guess.

    He used the "-NewVHDPath" that will create a new vhdx file for the new VM, so it needs to know the expected size for the drive.

    If he already has and existing vhdx that he wants to use, he should have use the "-VHDPath" option instead.

    Of course, this also means that the syntax sample in the help is missing the "-NewVHDSizeBytes" parameter.

    Cool posting Sean !! Thanks and I'll be looking for future ones.

  • Marianok

    Can't give away the secret ending but all I can tell you is that it involves "PowerShell" and getting home earlier :)

    "Shhhh....."

    Sean

    "The Energized Tech"

  • this is good but how would you create it so you install an operating system later?

  • Hey, so I know its been awhile since anyone has said anything on here, but here's my question. I have a script where I want to ask the user for input so I have $memory for example. User needs to input the value. My problem is this, I can't do it with (number)MB or (number)GB as it returns the System.Int64 Error : "Input string was not in correct format" With that being said, if I simply write out the entirety of new-vm and specify the memory in there I can put 2GB or 2MB. Likewise, if I simply assign the memory variable a value within the script it comes back as having been successful.