Last active
April 28, 2025 08:27
-
-
Save stelf/451d166103dd7390d38dffe36a8eb2ee to your computer and use it in GitHub Desktop.
Setup Win11 Contoso evaluation in Hyper-V
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check if running as Administrator | |
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
Write-Error "This script must be run as Administrator." | |
exit 1 | |
} | |
Import-Module Hyper-V | |
# Set variables for the VM creation | |
$VMName = "ContosoVM" | |
$VMPath = "C:\work\VMs\$VMName" | |
$ISOPath = "C:\Users\strix\Downloads\Contoso8_1_ENT.iso" | |
$VHDPath = "C:\work\VMs\$VMName\Virtual Hard Disks\$VMName.vhdx" | |
# Check if ISO file exists | |
if (-NOT (Test-Path $ISOPath -PathType Leaf)) { | |
Write-Error "ISO file not found at $ISOPath" | |
Write-Error perhaps get it from "https://www.microsoft.com/en-us/evalcenter/download-windows-11-enterprise" | |
exit 1 | |
} | |
$HyperVService = Get-Service -Name vmms -ErrorAction SilentlyContinue | |
if ($null -eq $HyperVService -or $HyperVService.Status -ne 'Running') { | |
Write-Error "Hyper-V Virtual Machine Management Service (vmms) is not running. Check BIOS/UEFI virtualization settings, Windows Features, and reboot." | |
exit 1 | |
} | |
# Check if base VM path exists (optional, as New-Item creates it later, but good for early failure) | |
$BaseVMPath = Split-Path $VMPath -Parent | |
if (-NOT (Test-Path $BaseVMPath -PathType Container)) { | |
Try { | |
New-Item -Path $BaseVMPath -ItemType Directory -Force -ErrorAction Stop | Out-Null | |
Write-Host "Created base VM directory: $BaseVMPath" | |
} Catch { | |
Write-Error "Failed to create base VM directory: $BaseVMPath. Check permissions." | |
exit 1 | |
} | |
} | |
# Create directory if it doesn't exist | |
New-Item -Path (Split-Path $VHDPath -Parent) -ItemType Directory -Force | |
# Check if VHD already exists | |
if (Test-Path $VHDPath -PathType Leaf) { | |
Write-Warning "VHD file already exists at $VHDPath. Skipping creation." | |
} else { | |
# Create a new 60GB fixed VHD for better performance | |
Try { | |
New-VHD -Path $VHDPath -SizeBytes 60GB -Fixed -ErrorAction Stop | |
Write-Host "Created VHD: $VHDPath" | |
} Catch { | |
Write-Error "Failed to create VHD at $VHDPath. $_" | |
exit 1 | |
} | |
} | |
# Create VM with enhanced settings | |
New-VM -Name $VMName -MemoryStartupBytes 6GB -Generation 2 -Path $VMPath -VHDPath $VHDPath | |
Set-VMProcessor -VMName $VMName -Count 4 -ExposeVirtualizationExtensions $true | |
Add-VMDvdDrive -VMName $VMName -Path $ISOPath | |
Set-VM -VMName $VMName -AutomaticCheckpointsEnabled $false -CheckpointType Disabled | |
Set-VMFirmware -VMName "ContosoVM" -EnableSecureBoot On | |
Set-VMKeyProtector -VMName "ContosoVM" -NewLocalKeyProtector | |
Enable-VMTPM -VMName "ContosoVM" | |
$dvd = Get-VMDvdDrive -VMName "ContosoVM" | |
Set-VMFirmware -VMName "ContosoVM" -FirstBootDevice $dvd | |
# Start VM | |
Start-VM -Name $VMName | |
# Set up external switch for Ethernet if not already present | |
$ethernetAdapter = Get-NetAdapter | Where-Object { $_.Status -eq "Up" -and $_.Name -eq "Ethernet" } | |
if ($ethernetAdapter) { | |
$switchName = "ExternalSwitch" | |
$existingSwitch = Get-VMSwitch | Where-Object { $_.SwitchType -eq "External" -and $_.NetAdapterInterfaceDescription -eq $ethernetAdapter.InterfaceDescription } | |
if (-not $existingSwitch) { | |
New-VMSwitch -Name $switchName -NetAdapterName $ethernetAdapter.Name -AllowManagementOS $true | |
} else { | |
$switchName = $existingSwitch.Name | |
} | |
# Connect VM to the external switch | |
Connect-VMNetworkAdapter -VMName $VMName -SwitchName $switchName | |
} else { | |
Write-Warning "No active Ethernet adapter found for bridging." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment