Ask Leo! by Leo A. Notenboom

Is there a way to "Safely Remove Hardware" from a batch file?

Search First! Then browse: Categories | Full Archive | By Date | Newsletter

Home » Hardware » USB and 1394 Interfaces

Summary: Automating the steps required to "safely remove hardware" isn't at all obvious, but it is possible.

Is there a way to "Safely Remove Hardware" from a batch file?

Many USB and Firewire devices can be removed from your computer simply by unplugging. Other devices aren't as easy because the operating system might need to complete an operation to shut the device down safely. Windows provides an icon in the taskbar that allows you to "Safely Remove Hardware" but if you want to automate the shutdown as part of a more complex process you need something more.

As an example, I recently purchased a Maxtor external hard drive and before I remove it I'd like to run a batch file that copies a few files around. It would be nice if that batch file could also invoke Windows "Safely Remove Hardware" when it finishes so I can then just unplug the drive.

Now it can.

Microsoft provides a utility called devcon for free download. It's a "Command Line Uility Alternative to Device Manager". It can actually do many things that I won't get into here, but removing a plug & play device is a simple operation once you know the unique name of the device you want to manipulate.

After you've downloaded devcon, run "devcon status *" in a command window. This will generate a list of all the devices on your system. Look for the line which describes your device. In my case for my external hard drive the (very cryptic) line looks like this:

SBP2\MAXTOR&ONETOUCH&LUN0\0010B9F700AFC279

That full line is a bit much but any substring that remains unique will do. I have another maxtor drive on my system but nothing else that has the phrase "sbp2\maxtor" so I'll use that. The command to safely remove my external drive then becomes:

devcon remove sbp2\maxtor*

The "*" at the end is a wildcard and matches the rest of the string.

Be certain to select the right device. In my case, installing the Maxtor drive actually installed three devices all labeled with "maxtor onetouch" - its firewire interface, its front panel interface, and finally the drive itself. It's the latter that I want to remove. Looking at the details of the "Safely Remove Hardware" dialog will help you select the appropriate entry.

Finally, be careful. Devcon can certainly cause problems if you delete the wrong device by mistake. A reboot or a re-run of the "add new hardware" wizard may restore you, but that's a risk I'd rather avoid.

Related:

Article C1883 - January 18, 2004

Helpful? Get new articles weekly by email in my FREE newsletter!

Your Name:
Your Email:


Why Subscribe?

Recent Comments
49 Comments

When i connect my usb pen drive on my laptop i see removal drive in my computer and work proparly but neither showing on taskbar for safaly remove nor get "usb device found massage" Please salve my problem.

Posted by: ANGAD at May 26, 2007 12:48 AM

Hi, this is a very helpfull page, thanks leo for all your efforts, and all the other people who have commented. I have the usb problem in reverse. I use wireless usb devices (for networking) and they appear in the safely remove hardware list. users sometimes accidentally remove the wireless device, and then the pc loses it's connection. Does anyone know of a way to remove a certain device from the list? so it CAN'T be turned off. cheers, jonnyxx.

Posted by: jonnyxx at August 25, 2007 11:50 PM

The comment Jared posted on Jan 20, 2007 is almost perfect, but I've adopted an easier way to use it. Copy the directory\command

%windir%\system32\RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll

Go to your desktop and create a new shortcut and paste that directory\command into the location bar. If you want the special Windows icon for it open up the properties of the shortcut, click Change Icon, click Browse, and (if it's already showing you your system32 directory) find hotplug.dll and open that. The Safely Remove Hardware icon is located in there.

I created this process for a novice user that liked to do everything from her desktop. The article suggests a much more complicated process, the icon/shortcut can be transferred to many XP computers. I'm just cutting out the middle man of the command prompt because c'mon man! this is Windows! not Linux! lol

Posted by: Chip at September 18, 2007 7:28 AM

Worked like a charm! Excellent article.

Posted by: Rockechris at October 9, 2007 1:08 AM

now able to see that unplag or eject hardware,

not showing usb device

able to see in mycomputer

Posted by: yogesh at November 28, 2007 11:37 PM

Sometimes i forget to use "Safely Remove Hardware" commands for my usb drive . Can this be any problem .

Posted by: Shrav at May 26, 2008 8:11 AM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yes. In the worst case you could corrupt and lose all the
data on the drive.

Leo

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)

iD8DBQFIOu2OCMEe9B/8oqERAqheAJ948wYoTwUYT2F7V2Yu96koZSMusACeN1OR
jHtBs3PzsTd7pikUo6brF8I=
=td4N
-----END PGP SIGNATURE-----

Posted by: Leo at May 26, 2008 10:04 AM

A program called USB Disk Ejector - is free and works perfectly!

Ian
.........

http://www.i-logo.net

Posted by: Ian at September 10, 2008 7:01 AM

Can it be used to remove bluetooth devices?

Posted by: DGlower at April 10, 2009 1:23 AM

Requires restart to remove esata connected drive that IS safely removable via dialogue. I hope it can come back after restart...

Posted by: Paul_Bags at October 17, 2009 1:26 PM

Post a comment on "Is there a way to "Safely Remove Hardware" from a batch file?":






(Email Address will not be published.)

Remember Me?

By popular demand...
my tip jar
Cuppa Joe
Buy Leo a Latte!

(you may use HTML tags for style)

RSS feed Subscribe to the RSS Feed specifically for comments on this article.

Before commenting, please...

  • Read the article at the top of this page. If your comment shows you didn't, it'll be deleted and ignored.

  • Comment only on this article. Use the Google search box at the top of the page if you have a question about something else.

  • Don't include personal information in the comment. No email addresses. No phone numbers. No physical addresses.

  • Don't spam. Excessive links to unrelated sites within a comment or across multiple comments will cause all such comments to be removed.

  • Don't ask me to recover lost passwords or hacked accounts. I can't, and those comments will be deleted.

  • I can't respond to every comment. And I can't vouch for the accuracy of others who do.

Please wait. Your comment is being processed ...


Question? Ask Leo!