# Check if USB drive exists if (-not (Test-Path $drivePath)) Write-Host "Drive $UsbDriveLetter does not exist or is not ready." -ForegroundColor Red exit 1
# Helper: Run diskpart script function Invoke-DiskPart Out-Null
# 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
if (-not $volNumber) Write-Host "Failed to parse volume number." -ForegroundColor Red exit 1 windows 7 portable usb
# Get disk number for the USB drive $diskInfo = diskpart /s (New-TemporaryFile | % $_.FullName; Set-Content $_.FullName "list volume`nexit" ) $diskNumberLine = $diskInfo | Select-String -Pattern "$driveLetter\s+" | Select-Object -First 1 if (-not $diskNumberLine) Write-Host "Could not identify disk number for $UsbDriveLetter" -ForegroundColor Red exit 1
# Wait for drive to be ready Start-Sleep -Seconds 3
# Make boot sector (bootsect.exe from Windows ADK or Windows 7 installation) $bootsect = "$env:SystemRoot\System32\bootsect.exe" if (Test-Path $bootsect) Write-Host "Updating boot sector for NTFS..." -ForegroundColor Cyan & $bootsect /nt60 $UsbDriveLetter /force /mbr else Write-Host "Warning: bootsect.exe not found. USB might not boot on older BIOS systems." -ForegroundColor Yellow # Check if USB drive exists if (-not
# Mount ISO (works on Windows 8+; on Windows 7 use alternative) $mountDrive = $null if ($PSVersionTable.PSVersion.Major -ge 6 -or [Environment]::OSVersion.Version.Major -ge 10) # Windows 10/11/Server 2016+ or PowerShell 6+ $mount = Mount-DiskImage -ImagePath $IsoPath -PassThru $mountDrive = ($mount else # Windows 7 fallback: use 7-Zip or mount via free tool? Better to guide user Write-Host "Windows 7 cannot natively mount ISO. Please extract ISO contents to a folder first." -ForegroundColor Yellow $extractedPath = Read-Host "Enter path to extracted ISO folder (or press Enter to use 7-Zip automatically if installed)" if (-not $extractedPath) Select-Object -First 1 if ($7z) $tempExtract = Join-Path $env:TEMP "win7usb_iso_extract" Remove-Item $tempExtract -Recurse -Force -ErrorAction SilentlyContinue New-Item -ItemType Directory -Path $tempExtract -Force else Write-Host "Please install 7-Zip or manually extract ISO contents and rerun." -ForegroundColor Red exit 1 else $mountDrive = $extractedPath
<# .SYNOPSIS Creates a bootable Windows 7 USB drive from an ISO file. .DESCRIPTION This script formats a USB drive, makes it bootable (MBR + NTFS), and copies all Windows 7 setup files from an ISO. .NOTES Version: 1.0 Author: Generated for Windows 7 USB creation Requires: Administrative privileges, USB drive (4GB+ for 32-bit, 8GB+ for 64-bit) #>
#Requires -RunAsAdministrator
$driveLetter = $UsbDriveLetter[0] $drivePath = $UsbDriveLetter + "\"
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
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 ) Please extract ISO contents to a folder first
# 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