Code to create Virtual Machine in Powershell
Procedure:
- 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-vmharddrive -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
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
Source Code:
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