Advertisement
tedeansiii

List/Format/Eject USB Flash Drives

Jun 3rd, 2022 (edited)
1,450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Written by Thomas Deans - thomas@thomasdeanstech.com - February, 2020
  2. # Note: Only insert Flash drives you want to be formatted. I was formatting in batches of 8 at a time
  3. Param(
  4. $Drives
  5. )
  6. $USBLabel = "1234567890"
  7. $FlashFileSysType = "FAT32"
  8. $NewFlashFileSysType = "exFAT"
  9. if ([string]::IsNullOrEmpty($Drives) -or [string]::IsNullOrWhiteSpace($Drives)) { $Drives = $false }
  10. if ($Drives -eq $false) {
  11. $Drive_Letters = (Get-Volume | ? { $_.FileSystemType -eq $FlashFileSysType } | ? { $_.DriveType -eq 'Removable' }).DriveLetter
  12. } else { $Drive_Letters = $Drives }
  13.  
  14. # Drive name must follow DOS 8 character limit
  15. foreach ($DL in $Drive_Letters) {
  16. Format-Volume -DriveLetter $DL -FileSystem $NewFlashFileSysType -NewFileSystemLabel $USBLabel.Substring(0,8) -Force -Verbose
  17. }
  18.  
  19. Get-Volume | ? { $_.FileSystemType -eq 'Unknown' } | ? { $_.DriveType -eq 'Removable' } | select DriveLetter,FileSystemType,FileSystemLabel,SizeRemaining
  20.  
  21. #Eject Flash Drives retainign drive letters
  22. foreach ($DL in $Drive_Letters) {
  23. Write-Verbose "Ejecting: $DL" -Verbose
  24. $driveEject = New-Object -comObject Shell.Application
  25. $driveEject.Namespace(17).ParseName("$($DL):").InvokeVerb("Eject")
  26. sleep -s 1
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement