合并PowerShell脚本实现USB可选FAT32/NTFS格式化及参数配置
Combined USB Formatting Script (FAT32/NTFS)
Got it, let's combine those two scripts into a single, flexible PowerShell tool that lets you choose between FAT32 and NTFS, plus set all the key parameters you mentioned. Here's a polished version with built-in safety checks and clear prompts:
cls Write-Host "=== USB Drive Format Tool ===" -ForegroundColor Cyan Write-Host "Listing all connected disks (identify your USB drive by size):`n" -ForegroundColor Gray # List disks to help user pick the correct USB drive "list disk" | diskpart # Get and validate Disk Number do { Write-Host -NoNewline "Enter the DISK number of your USB drive then press ENTER: " $inputString = Read-Host $diskNumber = $inputString -as [Int32] $isValidDisk = $diskNumber -ne $null -and $diskNumber -ge 0 if (-not $isValidDisk) { Write-Host "Error: Please enter a valid whole number for the disk number." -ForegroundColor Red } } while (-not $isValidDisk) # Select File System (FAT32/NTFS) do { Write-Host -NoNewline "Choose file system (FAT32/NTFS): " $fileSystem = Read-Host $fileSystem = $fileSystem.ToUpper() $isValidFS = $fileSystem -eq "FAT32" -or $fileSystem -eq "NTFS" if (-not $isValidFS) { Write-Host "Error: Please enter either 'FAT32' or 'NTFS'." -ForegroundColor Red } } while (-not $isValidFS) # Get Drive Letter do { Write-Host -NoNewline "Enter the desired drive letter (e.g., F): " $driveLetter = Read-Host $driveLetter = $driveLetter.ToUpper() $isValidLetter = $driveLetter -match "^[A-Z]$" if (-not $isValidLetter) { Write-Host "Error: Please enter a single uppercase letter (A-Z)." -ForegroundColor Red } } while (-not $isValidLetter) # Get Volume Label Write-Host -NoNewline "Enter the new volume label (leave blank for no label): " $volumeLabel = Read-Host # Critical Confirmation Step (prevents accidental data loss) Write-Host "`n=== Confirmation ===" -ForegroundColor Yellow Write-Host "Disk Number: $diskNumber" Write-Host "File System: $fileSystem" Write-Host "Drive Letter: $driveLetter`:" Write-Host "Volume Label: $(if ($volumeLabel -eq "") { "None" } else { $volumeLabel })" Write-Host -NoNewline "Type 'YES' to proceed with formatting (this will erase ALL data on the disk): " $confirmation = Read-Host if ($confirmation.ToUpper() -ne "YES") { Write-Host "Formatting cancelled." -ForegroundColor Green exit } # Build and run DiskPart commands $diskpartCommands = @" select disk $diskNumber clean create partition primary format fs=$fileSystem quick label="$volumeLabel" assign letter=$driveLetter exit "@ Write-Host "`nStarting formatting process...`n" -ForegroundColor Cyan $diskpartCommands | diskpart # Show final status if ($LASTEXITCODE -eq 0) { Write-Host "`nSuccess! Drive formatted as $fileSystem with letter $driveLetter`: and label '$volumeLabel'." -ForegroundColor Green } else { Write-Host "`nError: Formatting failed. Check disk permissions or ensure the drive is not in use." -ForegroundColor Red }
Key Details & Usage Tips:
- Safety Checks: The script validates every input to avoid typos, and requires explicit "YES" confirmation before erasing data—no accidental formatting!
- Flexibility: Choose FAT32 for cross-device compatibility (works with Windows, Mac, consoles) or NTFS for larger file support and Windows-specific features.
- Permissions: You must run PowerShell as Administrator to execute disk formatting commands—standard user accounts won't have access.
- Validation: The script checks that your disk number is a valid integer, drive letter is a single A-Z character, and file system is one of the two supported options.
内容的提问来源于stack exchange,提问作者Milke-m5




