# Public domain. <# Example: $1MiB = [Math]::Pow(2,20) $1GiB = [Math]::Pow(2,30) $vm_properties = @{ 'VMName' = 'my-vm'; 'ISOPath' = 'C:\path\to\my.iso'; 'UseLegacyNetworkAdapter' = $True; 'VHDLength' = (8 * $1GiB); 'VHDType' = 'Fixed'; 'MemoryMinimumBytes' = (4096 * $1MiB); 'MemoryMaximumBytes' = (4096 * $1MiB); 'MemoryStartupBytes' = (4096 * $1MiB); 'StartVM' = $True; }; $vm = C:\path\to\new-vm.ps1 @vm_properties # Get IP address of VM assigned to it by DHCP while ([String]::IsNullOrEmpty($vm.NetworkAdapters[0].IPAddresses)) { Start-Sleep -Seconds 30 } @{ 'IPAddress' = $vm.NetworkAdapters[0].IPAddresses[0]; 'GUID' = $vm.VMId.Guid; 'Name' = $vm.Name; } | Format-Table #> Param( [Parameter(Mandatory=$True)] [Alias('Name')] [String] $VMName, [Parameter(Mandatory=$False)] [String] $IsoPath = $null, [Parameter(Mandatory=$False)] [String] $VhdPath = [System.IO.Path]::Combine(${Env:PUBLIC}, 'Documents', 'Hyper-V', 'Virtual hard disks'), [Parameter(Mandatory=$False)] [UInt16] $ProcessorCount = 4, [Parameter(Mandatory=$False)] [UInt64] $VHDLength = 8 * [Math]::Pow(2,30), [Parameter(Mandatory=$False)] [ValidateSet('Fixed', 'Dynamic')] [String] $VHDType = 'Dynamic', [Parameter(Mandatory=$False)] [UInt64] $MemoryMinimumBytes = 2048 * [Math]::Pow(2,20), [Parameter(Mandatory=$False)] [UInt64] $MemoryMaximumBytes = 4096 * [Math]::Pow(2,20), [Parameter(Mandatory=$False)] [UInt64] $MemoryStartupBytes = $MemoryMinimumBytes, [Parameter(Mandatory=$False)] [UInt64] $Generation = 1, [Parameter(Mandatory=$False)] [Switch] $UseLegacyNetworkAdapter = $False, [Parameter(Mandatory=$False)] [Switch] $UseVhdxFormat = $False, [Parameter(Mandatory=$False)] [Switch] $StartVM = $True ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version 2 $timestamp = (Get-Date).ToString('yyyyMMdd_HHmmss') $vhd_file_name = "{0}-{1}.vhd" -f ($VMName, $timestamp) if ($UseVhdxFormat) { $vhd_file_name += 'x' } $vm_vhd_path = [System.IO.Path]::Combine($VhdPath, $vhd_file_name) $vm_switch_name = Get-VMSwitch | Select-Object -First 1 -ExpandProperty Name $vhd_properties = @{ 'Path' = $vm_vhd_path; 'SizeBytes' = $VHDLength; $VHDType = $True; }; New-VHD @vhd_properties | Out-Null $vm_create_properties = @{ 'Name' = $VMName; 'MemoryStartupBytes' = $MemoryStartupBytes; 'Generation' = $Generation; 'VHDPath' = $vm_vhd_path; }; $vm = New-VM @vm_create_properties # Remove the default unconfigured virtual network adapter and add your own: $vm_net_adapter = @{ 'VM' = $vm; 'IsLegacy' = [Boolean] $UseLegacyNetworkAdapter; 'SwitchName' = $vm_switch_name; } Remove-VMNetworkAdapter -VM $vm Add-VMNetworkAdapter @vm_net_adapter # Update virtual DVD drive with path to ISO: if (Test-Path -Path $IsoPath) { $dvd_drive = Get-VM -Id $vm.VMId | Get-VMDvdDrive Set-VMDvdDrive -VMDvdDrive $dvd_drive -Confirm:$False -Path $IsoPath } $vm_set_properties = @{ 'VM' = $vm; 'DynamicMemory' = $True; 'MemoryMinimumBytes' = $MemoryMinimumBytes; 'MemoryMaximumBytes' = $MemoryMaximumBytes; 'ProcessorCount' = $ProcessorCount; 'CheckpointType' = 'Standard'; 'AutomaticCheckpointsEnabled' = $False; }; Set-VM @vm_set_properties | Out-Null if ($StartVM) { Start-VM -VM $vm | Out-Null } Get-VM -Id $vm.VMId # END