Posts Eject USB Drive via CMD

Eject USB Drive via CMD

I have a project that I’m working on where’d I’d like to create an inexpensive (but reliable) backup solution for 20+ terabytes of information. This solution will be a combination of both hardware and software (scripts); where one key ingredient is the ability to eject multiple USB drives via a script (command-line) of some sort.

After searching, there doesn’t appear to be any built-in mechanism to do this in Windows; however, thanks to the wonderful tools over at SysInternals there appears to be an answer.

sync -e x:

This will:

  • Flush the data on the target drive/device.
  • Eject the device.
  • Doesn’t reset the drive letter (for reconnection later).
  • Doesn’t distort/restructure the USB tree.


The following information is being included in part for me to follow-up on at a later date as I continue to work through this project - first of which requires that I do some Arduino development/hardware hacking.


If you encounter file locks, you can utilize handle and/or procexp to determine what may be unexpectedly locking file(s).

Safely Remove Hardware Dialog

RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll


RunDll32.exe shell32.dll,Control hotplug.dll


I haven’t tried the following, but adding it here to explore at a later date:

$driveEject = New-Object -comObject Shell.Application

Replace X: with the desired drive letter.


I also found a project that might be something for me to explore further (possibly allowing me to do this from VB).

This post is licensed under CC BY 4.0 by the author.