Completion of gpu passthrough script from host pc to hypervisor vm

This script partially works but not fully. I removed my computer name so all you would have to do is copy and paste into chatgpt with you computer name and drive location.
<#
.SYNOPSIS
Final Hyper-V GPU-P Passthrough Script for NVIDIA RTX 4080 Laptop GPU.
.DESCRIPTION
Safely stops VM, injects patched NVIDIA drivers, configures MMIO & registry, reinstalls GPU partition adapter, starts VM ready to game at full performance.
#>

Param(
[string]$VMName = “yourvmname”,
[int]$GuestIoMB = 12288
)

1. Admin Check

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Error “Run this script as Administrator.”; exit
}

Write-Host “`n🚀 Starting GPU-P Setup for VM: $VMName” -ForegroundColor Cyan

2. Stop VM

$vm = Get-VM -Name $VMName -ErrorAction Stop
if ($vm.State -ne ‘Off’) {
Write-Host “Stopping VM…” -ForegroundColor Yellow
Stop-VM -Name $VMName -Force
do { Start-Sleep 2; $vm = Get-VM -Name $VMName } while ($vm.State -ne ‘Off’)
}
Write-Host “✅ VM is now Off.” -ForegroundColor Green

3. Configure Host Registry

$gKey = ‘HKLM:\SYSTEM\CurrentControlSet\Control\GraphicsDrivers’
New-ItemProperty -Path $gKey -Name ‘GpuVirtualizationFlags’ -PropertyType DWord -Value 1 -Force | Out-Null

$pKey = ‘HKLM:\SOFTWARE\Policies\Microsoft\Windows\HyperV’
if (-not (Test-Path $pKey)) { New-Item -Path $pKey -Force | Out-Null }
New-ItemProperty -Path $pKey -Name ‘RequireSecureDeviceAssignment’ -PropertyType DWord -Value 0 -Force | Out-Null
New-ItemProperty -Path $pKey -Name ‘RequireSupportedDeviceAssignment’ -PropertyType DWord -Value 0 -Force | Out-Null
Write-Host “✅ Host registry settings applied.” -ForegroundColor Green

4. Configure VM MMIO

Set-VM -Name $VMName -GuestControlledCacheTypes $true
-LowMemoryMappedIoSpace 1GB -HighMemoryMappedIoSpace 32GB
-AutomaticStopAction TurnOff
Write-Host “✅ VM MMIO configured.” -ForegroundColor Green

5. Mount VM Disk

$vhdPath = (Get-VMHardDiskDrive -VMName $VMName).Path
Mount-VHD -Path $vhdPath -Passthru | Out-Null
Start-Sleep 2

$guestDriveLetter = (Get-DiskImage -ImagePath $vhdPath | Get-Disk | Get-Partition | Get-Volume |
Where-Object { Test-Path “$($_.DriveLetter):\Windows\System32” }).DriveLetter
if (-not $guestDriveLetter) {
Dismount-VHD -Path $vhdPath
Write-Error “Cannot find System32 inside guest OS.”; exit
}
$guestSys32 = “$guestDriveLetter`:\Windows\System32”
Write-Host “✅ Guest OS mounted at $guestDriveLetter:” -ForegroundColor Green

try {
# 6. Copy NVIDIA Driver
$driverStorePath = “C:\Windows\System32\DriverStore\FileRepository”
$nvidiaFolder = Get-ChildItem -Path $driverStorePath -Directory |
Where-Object { $_.Name -like “nvcvamd64” } |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1

if (-not $nvidiaFolder) { throw "❌ No NVIDIA DriverStore found." }

$driverStoreGuest = "$guestSys32\HostDriverStore\FileRepository\$($nvidiaFolder.Name)"
New-Item -ItemType Directory -Path (Split-Path $driverStoreGuest) -Force | Out-Null
Copy-Item -Path $nvidiaFolder.FullName -Destination $driverStoreGuest -Recurse -Force
Write-Host "✅ Copied NVIDIA DriverStore." -ForegroundColor Green

# 7. Copy Critical NVIDIA DLLs
$dlls = @('nvapi64.dll','nvcuda.dll','nvencodeapi64.dll','nvd3dumx.dll','nvldumdx.dll','nvwgf2umx.dll','nvml.dll','nvoptix.dll')
foreach ($dll in $dlls) {
    if (Test-Path "$Env:SystemRoot\System32\$dll") {
        Copy-Item -Path "$Env:SystemRoot\System32\$dll" -Destination "$guestSys32\$dll" -Force
    }
}
Write-Host "✅ Copied NVIDIA core DLLs." -ForegroundColor Green

# 8. Optional: Copy NVIDIA Tools
$srcTools = "$Env:SystemRoot\System32\drivers\NVIDIA Corporation"
$dstTools = "$guestSys32\drivers\NVIDIA Corporation"
if (Test-Path $srcTools) {
    New-Item -Path $dstTools -ItemType Directory -Force | Out-Null
    Copy-Item -Path "$srcTools\*" -Destination $dstTools -Recurse -Force
    Write-Host "✅ NVIDIA tools copied." -ForegroundColor Green
}

# 9. Patch INF with Remove-HypervisorChecks.ps1
$removeScript = Get-ChildItem "C:\Program Files (x86)\Windows Kits\10\Tools" -Filter "Remove-HypervisorChecks.ps1" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1
if ($removeScript) {
    try {
        Set-ExecutionPolicy Bypass -Scope Process -Force
        & $removeScript.FullName -Nvidia -DriverPath $driverStoreGuest
        Write-Host "✅ Patched NVIDIA INF successfully." -ForegroundColor Green
    } catch {
        Write-Warning "⚠️ Failed to patch driver. Manual patch may be needed."
    }
} else {
    Write-Warning "⚠️ WDK script not found. Skipping patch."
}

# 10. Edit Guest Registry
& reg.exe load HKLM\TmpHive "$guestDriveLetter`:\Windows\System32\Config\SYSTEM" | Out-Null
$pvKey = 'HKLM:\TmpHive\ControlSet001\Control\GraphicsDrivers\Paravirtualization'
if (-not (Test-Path $pvKey)) { New-Item -Path $pvKey -Force | Out-Null }
New-ItemProperty -Path $pvKey -Name 'GuestIoSpaceSizeInMb' -Value $GuestIoMB -PropertyType DWord -Force | Out-Null
& reg.exe unload HKLM\TmpHive | Out-Null
Write-Host "✅ Set GuestIoSpaceSizeInMb=$GuestIoMB." -ForegroundColor Green

# 11. Install Driver into Guest
$infFile = Get-ChildItem -Path $driverStoreGuest -Recurse -Filter "*.inf" | Select-Object -First 1
if ($infFile) {
    pnputil.exe /add-driver "$($infFile.FullName)" /install
    Write-Host "✅ Installed driver into guest OS." -ForegroundColor Green
}

} finally {
Dismount-VHD -Path $vhdPath -Confirm:$false
Write-Host “✅ VHD unmounted.” -ForegroundColor Green
}

12. Reset GPU Partition Adapter

try {
Remove-VMGpuPartitionAdapter -VMName $VMName -Confirm:$false -ErrorAction SilentlyContinue
} catch {}
Add-VMGpuPartitionAdapter -VMName $VMName
Write-Host “✅ GPU Partition Adapter reattached.” -ForegroundColor Green

13. Final Step

$response = Read-Host “`n🔥 Setup complete! Start VM now? (Y/N)”
if ($response -match “[1]$”) {
Start-VM -Name $VMName
Write-Host “✅ VM Started. Login and test your games.” -ForegroundColor Green
} else {
Write-Host “Start VM manually with Start-VM -Name ‘$VMName’ later.” -ForegroundColor Yellow
}

Write-Host “n🏆 GPU-P Setup Fully Completed!" -ForegroundColor Cyan Read-Host "nPress ENTER to exit safely”


  1. Yy ↩︎