How can create and configure Virtual Machine in HYPER-V with Powershell

When you want to create a new Virtual machine you can do it with 2 ways. One is from HYPER-V Manager and the other is with Powershell.

The difference are that with powershell can automate the task base on your requirements and why not to create your templates with specific VM requirements.

Today i would like to explain how can create a new Virtual Machine with powerhell and do small changes base on your requirements.

 

Create new Virtual Machine 

Create a virtual Machine from HYPER-V Manager it's very easy and most of them you know how can do it. But with Powershell you can manage and do configuration quicker when you have lot of Virtual Machines .

It's not a day by day task but think about if you must create 5 VM's with the same or different specification in HYPER-V Host. It will take more time to do it from HYPER-V Manager instead with powershell.

So let's explain how can do it with powershell

Because you must run a script open the Powershell ISE Editor and run the following script

$vmname="vmfrompowershell1"
$cpu=2

$vm=@{
name=$vmname
memorystartupbytes=2147483648
generation=2
newvhdpath="F:\HYPER-V\$vmname\$vmname.vhdx"
newvhdsizebytes=10737418240
bootdevice="vhd"
path="F:\HYPER-V\$vmname"
}

new-vm @vm

Set-VMProcessor -VMName $vmname -Count $cpu

After finish run the following command to verify that the new Virtual Machine created

get-vm

 

 

Change Memory of the Virtual Machine

If you would like to increase/decrease memory of the Virtual Machine you can use the Set-VMMemory

Set-VMMemory -VMName vmfrompowershell1 -StartupBytes 4GB

 

Change Virtual Processors of the Virtual Machine

Now we can change how Virtual Processors can use the Virtual Machine.

The powershell command is Set-VMProcessor

Type the following command to increase the Virtual Processors to 4

Set-VMProcessor -VMName vmfrompowershell1 -Count 4

 

Enable Network Adapter of the Virtual Machine

When create a new Virtual Machine the Network Adapter is disconnected.

Use the Connect-VMnetworkadapter to connect the Netowork Adapter in the VM.

Connect-VMNetworkAdapter -vmname vmfrompowershell1

Add the Virtual Machine in Specific VLAN

If you want to add the VM in specific VLAN you can use the command Set-VMNetworkAdapterVlan

I will add the VM in VLAN 10

Set-VMNetworkAdapterVlan -VMName vmfrompowershell1 -Access -VlanId 121

 

if you think it's very easy to create a small script with all these specs and automate your task every time that you want to create a new VM.

You can do only small changes after create the VM like RAM or CPU of the VM. 

Except from Blogs you have the option to use video from technology platforms to learn Powershell.

Pluralsight is one of the best platforms that i have use to train myself through different courses or prepare for a Certification.

 

I invite you to follow me on Twitter , Google+ or Facebook. If you have any questions, send email to me at info@askme4tech.com.

Have a nice day!!

Tags