Post

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.

1
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.

Notes

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.

Locks

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

1
RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll

possibly

1
RunDll32.exe shell32.dll,Control hotplug.dll

Powershell

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

1
2
$driveEject = New-Object -comObject Shell.Application
$driveEject.Namespace(17).ParseName("X:").InvokeVerb("Eject")

Replace X: with the desired drive letter.

JScript

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.