Powershell – Hyper-V – Create and configure a new Hyper-V VM

Scripting the creation of a Hyper-V VM using New-VM, Set-VM, and Connect-VMNetworkAdapter.

Code description

  1. Specify the configuration settings as you see fit in the top variables
  2. The VM will be created
  3. The VM will have its Min and Max RAM set and the amount of CPUs
  4. The VM will have its Network Adapter set

Code

$VM_ServerName = "TestVM"
$VM_VHDPath = "C:\Hyper-V\VHD\TestVM_CDrive.vhdx"
[int64]$VM_VHDSize = 100GB
[int64]$VM_MinRAM = 2GB
[int64]$VM_MaxRAM = 4GB
$VM_Generation = 2
$VM_Processors = 2
$VM_SwitchName = "Microsoft Network Adapter Multiplexor Driver - Virtual Switch"

New-VM –Name $VM_ServerName –MemoryStartupBytes $($VM_MinRAM) –NewVHDPath $VM_VHDPath -NewVHDSizeBytes $VM_VHDSize -Generation $VM_Generation

Set-VM -VMName $($VM_ServerName) -DynamicMemory -MemoryMinimumBytes $VM_MinRAM -MemoryMaximumBytes $VM_MaxRAM -ProcessorCount $VM_Processors

Connect-VMNetworkAdapter -VMName $VM_ServerName -SwitchName $VM_SwitchName

NewVM.png


		
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