Windows 7 Portable Usb 【2026】
# Diskpart commands to clean, partition, format, set active, assign letter $diskpartCommands = @( "select disk $diskNumber", "clean", "create partition primary", "select partition 1", "active", "format fs=ntfs quick label=WIN7USB", "assign letter=$driveLetter", "exit" )
param( [Parameter(Mandatory=$true, HelpMessage="Path to Windows 7 ISO file")] [ValidateScript(Test-Path $_ -PathType Leaf)] [string]$IsoPath, [Parameter(Mandatory=$true, HelpMessage="USB drive letter (e.g., D: or E:)")] [ValidatePattern("^[A-Za-z]:$")] [string]$UsbDriveLetter )
$diskNumberLine = diskpart /s (New-TemporaryFile | % $_.FullName; Set-Content $_.FullName "select volume $volNumber`nlist disk`nexit" ) $diskNumber = $diskNumberLine | Select-String -Pattern "\*" | ForEach-Object $_ -replace '.*Disk (\d+).*', '$1' | Select-Object -First 1 if (-not $diskNumber) Write-Host "Failed to get physical disk number." -ForegroundColor Red exit 1 windows 7 portable usb
# Clean up mount if ($mountDrive -match "^[A-Z]:\\?$" -and $mountDrive -ne $tempExtract -and (Get-PSDrive -Name $mountDrive[0] -ErrorAction SilentlyContinue)) Dismount-DiskImage -ImagePath $IsoPath -ErrorAction SilentlyContinue elseif ($tempExtract -and (Test-Path $tempExtract)) Remove-Item $tempExtract -Recurse -Force -ErrorAction SilentlyContinue
# Stop on errors $ErrorActionPreference = "Stop" # Diskpart commands to clean, partition, format, set
Write-Host "USB disk number: $diskNumber" -ForegroundColor Green
# Wait for drive to be ready Start-Sleep -Seconds 3 # Diskpart commands to clean
Write-Host "=== Windows 7 Bootable USB Creator ===" -ForegroundColor Cyan Write-Host "ISO : $IsoPath" Write-Host "USB : $UsbDriveLetter" Write-Host "WARNING: All data on $UsbDriveLetter will be DESTROYED!" -ForegroundColor Yellow $confirm = Read-Host "Type YES to continue" if ($confirm -ne "YES") Write-Host "Aborted." -ForegroundColor Red exit 0
# Validate ISO exists if (-not (Test-Path $IsoPath)) Write-Host "ISO file not found: $IsoPath" -ForegroundColor Red exit 1
# Parse disk number (e.g., "* Volume 2 D ..." -> 2) $tokens = $diskNumberLine -split '\s+' $volNumber = $null for ($i = 0; $i -lt $tokens.Count; $i++) if ($tokens[$i] -match '^\d+$' -and $tokens[$i+1] -eq $driveLetter) $volNumber = $tokens[$i] break
# Validate USB drive letter if (-not ($UsbDriveLetter -match "^[A-Za-z]:$")) Write-Host "Invalid drive letter format. Use like D: or E:" -ForegroundColor Red exit 1
