Wednesday, June 3, 2015

New Hyper-V Virtual Machine with PowerShell

Here is the PowerShell for a new VM like the wizard does:
$vmName = "New Virtual Machine"
$vmPath = "C:\ProgramData\Microsoft\Windows\Hyper-V\"
New-VM -Name $vmName -Path "$vmPath\$vmName" -MemoryStartupBytes 512MB -NewVHDPath ` "$vmPath\$vmName\$vmName.vhdx" -NewVHDSizeBytes 127gb -Generation 1 -BootDevice CD
#new Gen 1 VM with files stored in $vmPath, with 512MB static RAM, with 1 CPU, with 127GB dynamic VHDX, #disconnected from network, with empty CD drive

Here is how to make other common configurations (one line at a time):
Set-VMDvdDrive -VMName $vmName -Path "k:\ISO\ISO.iso"; #add ISO file to existing drive
Set-VM -NewVMName $vmName -ProcessorCount 2 -DynamicMemory -MemoryMaximumBytes 4096MB #set dynamic memory and CPU
Set-VMNetworkAdapter -VMName $vmName -Name "SecretNet" #connect to network
Set-VM -NewVMName $vmName -AutomaticStartAction StartIfRunning -AutomaticStopAction ShutDown #change auto actions
Checkpoint-VM -Name $vmName #make a new snapshot/checkpoint