|
| 1 | +# ================================ |
| 2 | +# Minimal Windows 11 VHDX Builder |
| 3 | +# ================================ |
| 4 | +# Run as Administrator |
| 5 | + |
| 6 | +#Requires -Version 5.1 |
| 7 | +#Requires -RunAsAdministrator |
| 8 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'This script is not intended to have any outputs piped')] |
| 9 | + |
| 10 | +param |
| 11 | +( |
| 12 | + [Parameter( |
| 13 | + Mandatory = $true, |
| 14 | + HelpMessage = 'Drive letter where Windows 11 ISO is mounted (e.g., D:)' |
| 15 | + )] [string] $IsoDrive = 'D:', |
| 16 | + [Parameter( |
| 17 | + Mandatory = $true, |
| 18 | + HelpMessage = 'Index of the Windows 11 edition to install from the image (use /Get-ImageInfo to check)' |
| 19 | + )] [ValidateRange(1, 10)] [int] $ImageIndex = 1, |
| 20 | + [Parameter( |
| 21 | + Mandatory = $true, |
| 22 | + HelpMessage = 'Path to create the VHDX file (e.g., C:\MinWin11.vhdx)' |
| 23 | + )] [string] $VhdPath = 'C:\MinWin11.vhdx', |
| 24 | + [Parameter( |
| 25 | + Mandatory = $false, |
| 26 | + HelpMessage = 'Size of the VHDX in GB' |
| 27 | + )] [ValidateRange(20, [int]::MaxValue)] [int] $VhdSizeGB = 25, |
| 28 | + [Parameter( |
| 29 | + Mandatory = $false, |
| 30 | + HelpMessage = 'Name of the Hyper-V VM to create (optional)' |
| 31 | + )] [string] $VmName = 'MinWin11' |
| 32 | +) |
| 33 | + |
| 34 | +Write-Host '=== Step 0: Prepare paths and image info ===' -ForegroundColor Cyan |
| 35 | + |
| 36 | +# Determine install.wim or install.esd path |
| 37 | +$InstallWim = Join-Path $IsoDrive 'sources\install.wim' |
| 38 | +if (-not (Test-Path $InstallWim)) { |
| 39 | + $InstallWim = Join-Path $IsoDrive 'sources\install.esd' |
| 40 | +} |
| 41 | + |
| 42 | +# Verify image file exists |
| 43 | +if (-not (Test-Path $InstallWim)) { |
| 44 | + throw "Cannot find install.wim or install.esd on $IsoDrive. Mount a Windows 11 ISO and update `\$IsoDrive`." |
| 45 | +} |
| 46 | + |
| 47 | +Write-Host "Using image file: $InstallWim" -ForegroundColor Yellow |
| 48 | + |
| 49 | +Write-Host '=== Step 1: Create and initialize VHDX ===' -ForegroundColor Cyan |
| 50 | + |
| 51 | +# Create VHDX |
| 52 | +New-VHD -Path $VhdPath -SizeBytes ("${VhdSizeGB}GB") -Dynamic | Out-Null |
| 53 | + |
| 54 | +# Mount and initialize |
| 55 | +$disk = Mount-VHD -Path $VhdPath -Passthru |
| 56 | +Initialize-Disk -Number $disk.Number -PartitionStyle GPT | Out-Null |
| 57 | + |
| 58 | +# Create EFI + OS partitions |
| 59 | +$efiPartition = New-Partition -DiskNumber $disk.Number -Size 100MB -GptType '{C12A7328-F81F-11D2-BA4B-00A0C93EC93B}' -AssignDriveLetter |
| 60 | +$osPartition = New-Partition -DiskNumber $disk.Number -UseMaximumSize -AssignDriveLetter |
| 61 | + |
| 62 | +# Format partitions |
| 63 | +Format-Volume -Partition $efiPartition -FileSystem FAT32 -NewFileSystemLabel 'System' -Confirm:$false | Out-Null |
| 64 | +Format-Volume -Partition $osPartition -FileSystem NTFS -NewFileSystemLabel 'Windows' -Confirm:$false | Out-Null |
| 65 | + |
| 66 | +$EfiDrive = ($efiPartition | Get-Volume).DriveLetter + ':' |
| 67 | +$OsDrive = ($osPartition | Get-Volume).DriveLetter + ':' |
| 68 | + |
| 69 | +Write-Host "EFI drive: $EfiDrive OS drive: $OsDrive" -ForegroundColor Yellow |
| 70 | + |
| 71 | + |
| 72 | +Write-Host '=== Step 2: Apply Windows image to OS partition ===' -ForegroundColor Cyan |
| 73 | + |
| 74 | +# If using ESD, DISM can still apply directly |
| 75 | +dism /Apply-Image /ImageFile:$InstallWim /Index:$ImageIndex /ApplyDir:$OsDrive | Out-Null |
| 76 | + |
| 77 | + |
| 78 | +Write-Host '=== Step 3: Basic boot configuration ===' -ForegroundColor Cyan |
| 79 | + |
| 80 | +# Create boot files on EFI partition |
| 81 | +bcdboot "$OsDrive\Windows" /s $EfiDrive /f UEFI | Out-Null |
| 82 | + |
| 83 | + |
| 84 | +Write-Host '=== Step 4: Mount offline image for servicing ===' -ForegroundColor Cyan |
| 85 | + |
| 86 | +# Mount the OS volume as an offline image for DISM servicing |
| 87 | +$MountDir = 'D:\Mount_MinWin11' |
| 88 | +if (-not (Test-Path $MountDir)) { |
| 89 | + New-Item -ItemType Directory -Path $MountDir | Out-Null |
| 90 | +} |
| 91 | + |
| 92 | +# Use DISM to mount the offline image |
| 93 | +dism /Mount-Image /ImageFile:$InstallWim /Index:$ImageIndex /MountDir:$MountDir /ReadOnly | Out-Null |
| 94 | + |
| 95 | +# NOTE: |
| 96 | +# We will service the *applied* OS at $OsDrive, not the ISO image. |
| 97 | +# For feature removal, we target the offline OS with /Image:$OsDrive not /Image:$MountDir. |
| 98 | + |
| 99 | +Write-Host '=== Step 5: Remove optional features (offline) ===' -ForegroundColor Cyan |
| 100 | + |
| 101 | +# You can see available features with: |
| 102 | +# dism /Image:$OsDrive /Get-Features |
| 103 | + |
| 104 | +# Aggressive feature removal list (adjust to taste) |
| 105 | +$featuresToDisable = @( |
| 106 | + 'FaxServicesClientPackage', |
| 107 | + 'Printing-Foundation-Features', |
| 108 | + 'Printing-PrintToPDFServices-Features', |
| 109 | + 'Printing-XPSServices-Features', |
| 110 | + 'MSRDC-Infrastructure', |
| 111 | + 'Microsoft-Windows-Subsystem-Linux', |
| 112 | + 'MediaPlayback' , |
| 113 | + 'WindowsMediaPlayer', |
| 114 | + 'WorkFolders-Client', |
| 115 | + 'SMB1Protocol', |
| 116 | + 'WCF-Services45', |
| 117 | + 'WCF-TCP-PortSharing45', |
| 118 | + 'IIS-WebServerRole', |
| 119 | + 'IIS-WebServer', |
| 120 | + 'IIS-DefaultDocument', |
| 121 | + 'IIS-DirectoryBrowsing', |
| 122 | + 'IIS-HttpErrors', |
| 123 | + 'IIS-StaticContent', |
| 124 | + 'IIS-HttpRedirect', |
| 125 | + 'IIS-ApplicationDevelopment', |
| 126 | + 'IIS-ISAPIExtensions', |
| 127 | + 'IIS-ISAPIFilter', |
| 128 | + # "IIS-NetFxExtensibility45", |
| 129 | + 'IIS-ASPNET45', |
| 130 | + 'IIS-HealthAndDiagnostics', |
| 131 | + 'IIS-HttpLogging', |
| 132 | + 'IIS-LoggingLibraries', |
| 133 | + 'IIS-RequestMonitor', |
| 134 | + 'IIS-HttpTracing', |
| 135 | + 'IIS-Security', |
| 136 | + 'IIS-RequestFiltering', |
| 137 | + 'IIS-IPSecurity', |
| 138 | + 'IIS-Performance', |
| 139 | + 'IIS-HttpCompressionStatic', |
| 140 | + 'IIS-WebServerManagementTools', |
| 141 | + 'IIS-IIS6ManagementCompatibility', |
| 142 | + 'IIS-Metabase', |
| 143 | + 'IIS-HostableWebCore' |
| 144 | +) |
| 145 | + |
| 146 | +foreach ($feature in $featuresToDisable) { |
| 147 | + Write-Host "Disabling feature: $feature" -ForegroundColor DarkYellow |
| 148 | + dism /Image:$OsDrive /Disable-Feature /FeatureName:$feature /Remove | Out-Null |
| 149 | +} |
| 150 | + |
| 151 | +Write-Host '=== Step 6: Remove provisioned apps (offline) ===' -ForegroundColor Cyan |
| 152 | + |
| 153 | +# Remove all provisioned appx packages except Store and framework (adjust as needed) |
| 154 | +$ProvisionedApps = dism /Image:$OsDrive /Get-ProvisionedAppxPackages | Select-String 'PackageName' |
| 155 | +foreach ($line in $ProvisionedApps) { |
| 156 | + $pkg = $line.ToString().Split(':')[1].Trim() |
| 157 | + if ($pkg -notlike '*Store*' -and $pkg -notlike '*NET*' -and $pkg -notlike '*AppInstaller*') { |
| 158 | + Write-Host "Removing provisioned app: $pkg" -ForegroundColor DarkYellow |
| 159 | + dism /Image:$OsDrive /Remove-ProvisionedAppxPackage /PackageName:$pkg | Out-Null |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +Write-Host '=== Step 7: WinSxS cleanup and image optimization ===' -ForegroundColor Cyan |
| 164 | + |
| 165 | +# Component store cleanup to reduce size |
| 166 | +dism /Image:$OsDrive /Cleanup-Image /StartComponentCleanup /ResetBase | Out-Null |
| 167 | + |
| 168 | +Write-Host '=== Step 8: Unmount temporary mount and clean up ===' -ForegroundColor Cyan |
| 169 | + |
| 170 | +# Unmount DISM image |
| 171 | +dism /Unmount-Image /MountDir:$MountDir /Discard | Out-Null |
| 172 | +# Remove mount directory |
| 173 | +Remove-Item $MountDir -Recurse -Force | Out-Null |
| 174 | + |
| 175 | +# Dismount VHD (you can leave it mounted if you want to inspect it) |
| 176 | +Dismount-VHD -Path $VhdPath |
| 177 | + |
| 178 | +Write-Host '=== Step 9: (Optional) Create a Hyper-V VM using this VHDX ===' -ForegroundColor Cyan |
| 179 | + |
| 180 | +# Create a Hyper-V VM if Hyper-V module is available |
| 181 | +if (Get-Command New-VM -ErrorAction SilentlyContinue) { |
| 182 | + if (-not (Get-VM -Name $VmName -ErrorAction SilentlyContinue)) { |
| 183 | + New-VM -Name $VmName -MemoryStartupBytes 2GB -Generation 2 -VHDPath $VhdPath | Out-Null |
| 184 | + Set-VMFirmware -VMName $VmName -FirstBootDevice (Get-VMFirmware -VMName $VmName).BootOrder[0] |
| 185 | + Write-Host "Created Hyper-V VM '$VmName' using $VhdPath" -ForegroundColor Green |
| 186 | + } else { |
| 187 | + Write-Host "Hyper-V VM '$VmName' already exists. Attach $VhdPath manually if needed." -ForegroundColor Yellow |
| 188 | + } |
| 189 | +} else { |
| 190 | + Write-Host "Hyper-V module not available. Create a VM manually and attach $VhdPath." -ForegroundColor Yellow |
| 191 | +} |
| 192 | + |
| 193 | +Write-Host "=== DONE. Minimal Windows 11 VHDX created at $VhdPath ===" -ForegroundColor Green |
0 commit comments