Creating a VM in Hyper-V using Powershell

See the PS script below that will create a VM in Hyper V

Function Create-VM
{
param (
$Path = “C:\demo\virtual_machines”,
$Memory = “1024MB”,
$ISOPath = “e:\Image-name.ISO”,
[Parameter(Mandatory=$True,Position=1)]
[string]$VMName
)

# Create New VM
New-VM -Name $VMName -Path $Path -MemoryStartupBytes 1024MB -SwitchName “Name of your switch- if needed”

# Create new vhd *if needed*
$VHDPath = $Path+”\”+$VMName+”\”+$VMName+”.vhdx”
New-VHD -Path $VHDPath -Dynamic -SizeBytes 30GB

# Attach HDD
# You may attach a new VHD created above, or attach an existing VHD
#$VHDPath = “E:\Your-Disk.vhdx”
Add-VMHardDiskDrive -VMName $VMName -path $VHDPath

# Set dvd drive
# If you have a blank VHD newly created in above step, you will need an ISO image
Set-VMDvdDrive -VMName $VMName -ControllerNumber 1 -Path $ISOPath

# Start VM
Start-VM -Name $VMName
}

Create-VM “MyHyperV-VM-1”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s