How to Create a Hyper-V VM Using PowerShell

Share

In this article, we’ll explain how to create a Hyper-V VM quickly and easily using PowerShell 

Deploying new servers or creating virtual machines (VMs) can be quite a boring task, particularly if you have deploy multiple servers that are largely similar or all the same.

That’s why we’ve written this article to show you how you can use PowerShell to deploy your VMs with a few simple commands. You can also use these commands to create scripts so you can automate the process in the future.

 

How to Create a Hyper-V VM Using PowerShell

Basic PowerShell Commands for Creating a VM

PowerShell offers countless commands for use with multiple processes and services, and that includes Hyper-V! Even though most processes and services have a graphic interface, PowerShell can really speed things up to make life easier for sysadmins.

NOTE: In the summary at the end, we’ve included some links to other articles about Hyper-V and PowerShell in case you’re interested in learning more.

In this case, since we’re talking about creating a new Hyper-V VM, we’ll start with the “New-VM” command. This command has quite a rich syntax and there are many different available parameters:

New-VM
[[-Name] <String>]
[[-MemoryStartupBytes] <Int64>]
[-BootDevice <BootDevice>]
[-SwitchName <String>]
-NewVHDPath <String>
-NewVHDSizeBytes <UInt64>
[-Path <String>]
[-Version <Version>]
[-Prerelease]
[-Experimental]
[[-Generation] <Int16>]
[-Force]
[-AsJob]
[-CimSession <CimSession[]>]
[-ComputerName <String[]>]
[-Credential <PSCredential[]>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]

Don’t worry, we’re not going to look at everything here. We’re going to keep things simple and have a look at a few of these parameters, as well as show you some examples.

However, before we do anything else, as we mentioned in a previous article, it’s important to first make sure that all the PowerShell help files are up to date. You can do this using the “Update-Help” command. Once updated, if you need more information about any of the available commands, you can simply run the command “Get-Help <PowerShell_Command>”.

Image - Updating PowerShell help files using the Update-Help command
Image – Updating PowerShell help files using the Update-Help command

 

Creating a Hyper-V Virtual Machine

As with anything else in life, it’s best to start with the simple things first. So, we’ll first create a VM assigning just a name and allocating the startup memory.

# New-VM -Name “<Name>” -MemoryStartupBytes <RAM>

Where:

  • <Name>: is the name of the VM.
  • <RAM>: is the amount of startup memory you want to allocate this. You can express this in bytes, KB, MB or GB, but as you might have suspected, doing it in GB keeps things simple.

Here’s an example:

# New-VM -Name “VMTests1” -MemoryStartupBytes 1GB

Image - PowerShell command to deploy a simple VM
Image – PowerShell command to deploy a simple VM

NOTE: You should always assign a name to your VMs and make sure that it makes sense to you in the future. It’s also important not to rename your VMs all the time as the name will be used to generate the names of VHDs, etc.

With this command, you can create a VM, but it won’t be running or have anything installed. It also won’t have disks, network configurations, etc. However, this command can be useful for deploying VMs en masse to then make changes to them later.

Let’s look at some of the other parameters.

Here, we’re going to specify the nameamount of memoryhard disk size, VM location and the location of the virtual hard drive (VHD/VHDX). To do this, we’ll use the following command:

# New-VM -Name “<Name>” -MemoryStartupBytes <RAM> -Path <VM_Location> -NewVHDPath <Disk_Location> -NewVHDSizeBytes <Disk_Size>

Where:

  • <Name>: is the name of the VM.
  • <RAM>: is the amount of memory in GB.
  • <VM_Location>: is where the VM is located.
  • <Disk_Location>: is where the disk is located, normally a level below the VM.
  • <Disk_Size>: is the size of the hard disk in GB.

And if we take our previous example and add these parameters, it would look like this:

# New-VM -Name “<Name>” -MemoryStartupBytes <RAM> -Path <VM_Location>  -NewVHDPath ‘c:\vhd\base.vhdx’ -NewVHDSizeBytes 40GB

Image - Single command to deploy a new VM
Image – Single command to deploy a new VM

We can also add other parameters, including:

  • Generation <Generation_Number>: is the VM generation, 1 or 2.
  • BootDevice <Boot_Device>: is the device that will boot the VM.
  • SwitchName <Virtual_Switch_Name>: is the virtual network device that the VM will use.
Image - Single PowerShell command to deploy a new VM
Image – Single PowerShell command to deploy a new VM

These are the most basic commands for creating a VM in Hyper-V. And with these commands, you can easily create a script for deploying multiple VMs at once. But that will have to wait for a future article…

 

Summary

As you can see, creating a Hyper-V VM with PowerShell is really a very efficient way of deploying your virtualised infrastructure, especially if you have to create multiple similar machines at once. Using PowerShell is not only more convenient; it’ll save you a lot of time too.

Of course, you can perform this task using the Hyper-V graphic interface, but once you learn the commands, PowerShell is certainly quicker. It might seem daunting at first, but we can assure you, the learning curve is really very smooth and definitely worth the time and effort.

If you’re interested in learning more about Hyper-V or PowerShell, check out our articles How to Install Hyper-V on Windows 10 or PowerShell Commands for Hyper-V, where you’ll find more commands you can use to make life easier. We’ve also written another article on how to Use PowerShell to Manage Hyper-V Checkpoints.

In fact, we’ve got a range of articles and tutorials on Hyper-V that you can find on our blog.

Thanks for reading!

Category:Cloud and Systems

Other posts that may interest you

18 de December de 2023
Our Remote Desktop service just keeps getting better! We’ve recently developed some new features to improve service security and
15 de December de 2023
On 22 November, the Jotelulu Roadshow 2023  finally came to a close after seven fantastic events in seven different
12 de September de 2023
Today, we’re going to tell you all about RAID. We’ll explain exactly what it is, take a look at the

Fill out the form and one of our Sales team will contact you soon.

growth@jotelulu.com  |  jotelulu.com 

You can unsubscribe from these communications at any time. For more information,  check our Privacy Policy.