Skip to content

Instantly share code, notes, and snippets.

@asears
Created November 2, 2024 12:57
Show Gist options
  • Save asears/ff769fc750e4b67c08b7153d920f1412 to your computer and use it in GitHub Desktop.
Save asears/ff769fc750e4b67c08b7153d920f1412 to your computer and use it in GitHub Desktop.
Run Windows 11 in VM
#requires -RunAsAdministrator
# .\Setup-Windows11-VM.ps1 -VMName 'W11' -SwitchName 'Default Switch' -ISOFile 'C:\Users\jeffmill\Downloads\ISO\Windows 11 - Version 22H2.iso' -VMPath 'E:\VM'
Param(
# Virtual Machine Name
[Parameter(Mandatory = $true)][string]$VMName,
# Virtual Switch to be used - see Get-VMSwitch
[Parameter(Mandatory = $true)][string]$SwitchName,
# Full path for the install media (ISO file)
[Parameter(Mandatory = $true)][string]$ISOFile,
# Path to store the VM
[Parameter(Mandatory = $true)][string]$VMPath
)
if (Get-VM -Name $VMName -ErrorAction Silent) {
Write-Warning "VM '$VMName' already exists."
return
}
$VhdPath = "{0}\{1}\Virtual Hard Disks\{2}.vhdx" -f $VMPath, $VMName, (Split-Path -Leaf $ISOFile)
if (Test-Path $VhdPath) {
Write-Warning "VHD '$VhdPath' already exists."
return
}
New-VM `
-Name $VMName `
-Generation 2 `
-MemoryStartupBytes 4096MB `
-SwitchName $SwitchName `
-Path $VMPath `
-NewVHDPath $VhdPath `
-NewVHDSizeBytes 64GB
Set-VM `
-Name $VMName `
-ProcessorCount 4 `
-AutomaticCheckpointsEnabled $false
Add-VMDvdDrive `
-VMName $VMName `
-Path $ISOFile
$DVDDrive = Get-VMDvdDrive -VMName $VMName
Set-VMFirmware -BootOrder $DVDDrive -VMName $VMName
Set-VMKeyProtector -VMName $VMName -NewLocalKeyProtector
Enable-VMTPM -VMName $VMName
'Now start VM, connect and press any key to boot from DVD.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment