Create and Configure Virtual Machine with PowerShell

 

This is my first article for 2016. If you read my articles you will have realize that last months i have give more attention in Virtualization and Powershell. This isn't mean that i willn't cover and others subjects like Exchange Server , Windows Servers and more. 

Today i decide to write an article that is continue one of my old article Create Virtual Machine in Windows 8.1 with HYPER-V with one difference.Now i work in Windows Server 2008 R2 HYPER-V Host instead of Windows 8.1.

I thougt some IT Pro are fan with Powershell or someone else think to proceed with Core Installation of HYPER-V and all of the tasks must be done from commands. So Today i will cover the creation and basic configuration of Virtual Machine from PowerShell instead of GUI.

Let's Start the action.

Today i will cover the following sections:

  • Create New Virtual Machine
  • Create Virtual Hard Disk
  • Attach Virtual Hard Disk in the Virtual Machine
  • Connect  Network Adapter in Virtual Machine 
  • Specify ISO to boot the VM

 

Create New Virtual Machine

Let's go to create the new virtual machine. Type

new-vm -vmname test -memorystartupbytes 1GB

Create Virtual Hard Drive

new-vhd -path "C:\users\Public\Documents\Hyper-v\Virtual Hard Disks\test.vhdx" -sizebytes 20GB -fixed

Use the -fixed to determine that your Virtual Hard Drive will be fixed. Because by default use Dynamic Virtual Hard Disk Type.

After create the Virtual Hard Disk you will see the results to verify the type of your VHD.

Attach Virtual Hard Drive in the Virtual Machine

add-vmharddiskdrive -vmname test -path "C:\users\Public\Documents\Hyper-v\Virtual Hard Disks\test.vhdx" -controllertype IDE -controllernumber 0 -location 1

Connect Network Adapter in Virtual Machine

Find the name of the Network Adapter for the new Virtual Machine

get-vmnetworkadapter -vmname test

Identify the name of the Virtual Switch from HYPER-V Host  that will be connect.

get-vmswitch

Connect the network adapter of the new Virtual Machine with the Virtual Switch. So now your Virtual Machine is connected in the network that use the Virtual Switch.

connect-vmnetworkadapter -vmname test -vmnetworkadaptername "network adapter" -switchname  "new virtual switch".

Verify the connection with 

get-vmnetworkadapter -vmname test

You can read more details related with Virtual Switch / Virtual Ethernet Adapters and how can isolate the traffic for VM's with powershell which you can't do it from GUI.

Specify ISO to boot the VM

set-vmdvddrive -vmname test -controlnumber 1 -path "C:\users\user\Documents\ISO\Windows8.1 Professional_w_SP1_x64.ISO"

Start the VM to install the Windows

start-vm -vmname test

 

This article explain only basic commands how can create a new Virtual Machine and configure it. You can find lot of powershell commands for HYPER-V in 

https://technet.microsoft.com/en-us/library/hh848559.aspx 

If you have any question you can do your comments through our commented system

Have a nice Weekend !!

 

Tags