Search code examples
batch-filecmdusb

What .BAT command to use to eject a USB drive (dismount, remove - you pick the proper verb)


From a .BAT file, how do you eject a USB drive knowing the drive letter, e.g. trying to eject e:

@echo off
rem What is this command?
EJECT e:
if errorlevel 1 goto could_not_eject
  echo Success!
  goto end
:could_not_eject
  echo Unable to eject e:
  goto end
:end

Solution

  • Try the ejectjs.bat :

    ::to eject specific dive by letter
    call ejectjs.bat G
    ::to eject all drives that can be ejected
    call ejectjs.bat *
    

    There's no built-in command that can do this.

    You can try also with powershell (it didn't worked for me):

    powershell and the wmi class:

    powershell "(get-wmiobject -Class Win32_Volume | where{$_.DriveLetter -eq 'G:'}).Dismount($true,$false)"
    

    or the wmic command :

    wmic path Win32_Volume where DriveLetter="G:" call Dismount
    

    it didn't worked for me. You can also use wmi classes through jscript but I haven't try it. Looks like only invoke verb solution works without problems.