# 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
# 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
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 ) windows 7 portable usb
#Requires -RunAsAdministrator
# 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" )
# 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) Get-Volume).DriveLetter + ":" 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) Where-Object Test-Path $_ else $mountDrive = $extractedPath # Validate USB drive letter if (-not ($UsbDriveLetter
# Stop on errors $ErrorActionPreference = "Stop"
# 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
# Validate ISO exists if (-not (Test-Path $IsoPath)) Write-Host "ISO file not found: $IsoPath" -ForegroundColor Red exit 1 Please extract ISO contents to a folder first
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
# 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
Write-Host "Preparing USB drive (clean, partition, format NTFS, set active)..." -ForegroundColor Yellow Invoke-DiskPart -Commands $diskpartCommands
# Helper: Run diskpart script function Invoke-DiskPart param([string[]]$Commands) $script = [string]::Join("`r`n", $Commands) + "`r`nexit" $script