Use command line to safely remove USB drives

USB flash drives are a supremely convenient way to store and transfer information. Easy to carry around, they allow you to plug in to almost any convenient PC, and access your data and programs, before disconnecting and moving on to the next system.

Except, of course, there is one potential complication. If you remove a USB drive that has a document open, or where some information is yet to be written, then you’ll probably lose that data (and maybe corrupt the file).

To avoid this you’re supposed to open your system tray, find and right-click the "Safely remove hardware" icon, then find and choose the icon representing the USB drive. And Windows will then make sure it’s safe for the drive to be unplugged. If you find this a little tedious, though, or you just forget occasionally, you might prefer to use RemoveDrive, a command-line tool that will programmatically prepare the drive you specify for safe removal.

The program is very easy to use. If your USB drive is always drive K:, say, then all you have to do is create a shortcut to run RemoveDrive like this:

RemoveDrive K:

And the program will try to prepare the drive for removal. This may fail, though, if for instance a document is open on the drive, so it’s generally better to use the loop switch (-L), like this:

RemoveDrive K: -L

RemoveDrive will then keep trying to prepare the drive for removal until it’s actually possible. Which also means its program window will stay open, and you’ll have a visual indicator that very obviously shows that it’s not safe to unplug the drive yet.

Of course USB drives will often be assigned different drive letters, but fortunately the author has thought of that, too. As well as a drive letter, RemoveDrive can accept the name of a drive, either in full or using wildcards, like this:

RemoveDrive "Corsair UFD USB Device"
RemoveDrive *corsair*

The first option looks for a drive called "Corsair UFD USB Device"; the second looks for a drive with "corsair" anywhere in the drive name. (Again, it’s probably a good idea to add a -L loop switch for the best results.)

The program has many other switches and options available, if you’re interested. It can stop the Windows indexing service for a moment, if you like, useful if removal is blocked because Windows is indexing the drive. And RemoveDrive supports ErrorLevel, useful if you plan to build it into a script or batch file. The text file bundled with the executable has all the details.

The real bonus comes in the program’s portability, though. You don’t need administrative rights to run it. And you’re able to launch the program from the USB drive you’re trying to remove, and it’ll copy a temporary file to the host system to make this happen. That’s not bad for a 41KB executable (32-bit version), and more than justifies RemoveDrive‘s place in your portable toolkit.

Photo Credit: chien321/Shutterstock

5 Responses to Use command line to safely remove USB drives

© 1998-2024 BetaNews, Inc. All Rights Reserved. Privacy Policy - Cookie Policy.