Jump to content


  • 0
anyweb

How can I quickly create a Hyper-V Virtual Machine using PowerShell

Question

Introduction

Update: This script was updated 2017/11/10 with several new abilities to customize most of what you can define in the Virtual Machine settings.

This is an extremely quick post to help you create Hyper-V virtual machines using PowerShell. The script assumes you've already installed the Hyper-V feature in Windows.


You can define a bunch of variables *highlighted in red below* such as type of Checkpoint, or Memory or CPU settings, to define how your Virtual Machines are created,

define variables.png

The script prompts you for three inputs:

  • Virtual Machine name
  • Virtual Switch Name
  • Generation type (Gen1=legacy,  Gen 2=UEFI)

Here's a screenshot of the script in action:

running script.png

And after running you can see the Virtual Machine properties match what you specified (Gen 2, switch name, cpu settings and so on...)

vm properties.png

and the New Virtual Switch is created (if it didn't already exist)

new virtual switch.png

that's it, have fun.

Downloads

You can download a copy of the script here.

Create HyperV VM2.ps1

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Hi guys,

Last night, I was rebuilding my labs and figured out that we could improve the script to be fully automated.

As I'm using differencing disks, I wanted to add support for that. If you would like to know more about differencing disks and how to use them, please reach out and I will be happy to help!

Let's start with some variables that I added and/or modified to get this thing started.

image.png.6b6a9c3bba98f24acf109bb0a6c1f4a9.png

With the $scriptPath variable, I let the script know in which folder it is located. That make things easy to import my csv file.

$scriptPath = split-path -parent $MyInvocation.MyCommand.Path
$Names = Import-csv $scriptPath\ServerNames.csv

The $ParentDisk variable is a hardcoded link to the Parent Disk used for differencing.
$ParentDisk = "G:\Keduco\BaseDisk\W2K16_Parent.vhdx"

The variables below are changed from Read-Host to a hardcoded value.

TIP: It would be even more clean to put the variables below in the csv file as well. ? 

$SwitchName =  "Keduco - VLAN1 - 192.168.1.x"
$Generation =  "2"

Now that we have set all our variables, it's time to import the csv file and fill the variable $Name

image.png.bd85e27e4129318e50f82b2c40e23c19.png

Once our $Name variable is set, we need to focus on the real job... Creating our VMs. That's where I changed things a little bit.

I reversed the check dir to store the VM procedure, this because it breaks if the folder allready exist. If we check if the folder is missing, it will first create the Folder and the VM.

Does the folder exist, it will just skip and continue with the rest of the VMs. So you can easely add or remove VMs from your csv input file, you can re-run the script as many times as you want, cause it will skip what is created and create what is added to your csv file.

I'm sure that you can still improve the script a lot, I mean using a csv to create different types of VMs based on different switches, different generations etc... 

image.thumb.png.9a5e3bfb7a2aa401ae1073ca44f8389a.png

I created a zip file with the modified script and csv file.

Feel free to reach out for help if needed!

Best Regards,

Ken

CreateVMs.zip

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.