Skip to content

Instantly share code, notes, and snippets.

@MrChrisWeinert
Last active January 24, 2023 17:04
Show Gist options
  • Save MrChrisWeinert/4a29c7923adfb442d46f3f35697253ca to your computer and use it in GitHub Desktop.
Save MrChrisWeinert/4a29c7923adfb442d46f3f35697253ca to your computer and use it in GitHub Desktop.
#Create the list of base images to select from
$Ubuntu = New-Object System.Management.Automation.Host.ChoiceDescription '&Ubuntu', 'OS: Ubuntu 20.04.3 Desktop'
$Win10 = New-Object System.Management.Automation.Host.ChoiceDescription 'Win1&0', 'OS: Windows 10'
$Win11 = New-Object System.Management.Automation.Host.ChoiceDescription 'Win1&1', 'OS: Windows 11 Updated January 2022'
$Server = New-Object System.Management.Automation.Host.ChoiceDescription '&Server', 'OS: Windows Server 2022 Updated January 2022'
$options = [System.Management.Automation.Host.ChoiceDescription[]]($Ubuntu, $Win10, $Win11, $Server)
$VMName = Read-Host("Please enter your VM Name")
$OS = $Host.UI.PromptForChoice("","Please select an Operating System",$options, 0)
switch ($OS)
{
0 {$OS="Ubuntu"}
1 {$OS="Win10"}
2 {$OS="Win11"}
3 {$OS="Server"}
}
if ($OS -eq "Ubuntu")
{
$ParentPath = "Ubuntu_20.04.3_Desktop_Base"
}
elseif ($OS -eq "Win10")
{
$ParentPath = "Windows10_Base"
}
elseif ($OS -eq "Win11")
{
$ParentPath = "Win11_Base_updated_jan_2022"
}
elseif ($OS -eq "Server")
{
$ParentPath = "Svr22_Base_updated_jan_2022"
}
#Create Differencing Disk
New-VHD -Path "C:\VM\$VMName\Virtual Hard Disks\$VMName.vhdx" -ParentPath "C:\VM\$ParentPath\Virtual Hard Disks\$ParentPath.vhdx" -Differencing
#Create new VM
New-VM -Name $VMName -MemoryStartupBytes 4GB -BootDevice VHD -VHDPath "C:\VM\$VMName\Virtual Hard Disks\$VMName.vhdx" -Path "C:\VM" -Generation 2 -SwitchName "vWired"
Set-VM -VMName $VMName -ProcessorCount 4 -DynamicMemory -CheckpointType Disabled
Enable-VMIntegrationService -Name "Guest Service Interface" -VMName $VMName
if ($OS -eq "Ubuntu")
{
Set-VMFirmware -VMName $VMName -EnableSecureBoot Off
Set-VM -VMName $VMName -EnhancedSessionTransportType HvSocket
}
#Optional: Enable Nested Hyper-V
#Set-VMProcessor -VMName $VMName -ExposeVirtualizationExtensions $true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment