Ask Leo!

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

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 141 | Posted January 18, 2004

Recent Comments
0 Comments

Hey Leo, thanks.

Devcon works like a charm.

Now, I wondered.. if a small utility like this can romeve my eSata external harddisk drive, connected by SiImage Sata controller....
Is there any way by messing with the registry, to place/show my eSata drive under "Safely Remove Hardware" in XP (where it is somehow not present now)?

Thanks in advance,
Jumme

Posted by: Jumme (Denmark) at April 24, 2007 6:36 PM

When i connect my USB FLASH, drive don't show in my computer!!! Show the message that is all ok but isn't help!

Posted by: pop4e at May 24, 2007 1:48 PM

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

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)

New!

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

Before commenting, please...

Please wait. Your comment is being processed ...


Ask Your Question:


ask-leo.com
Web

Stay Informed

Weekly Newsletter

Archives

By Category
By Date

Advertisers

Advertise on Ask Leo!

««   »»

Question? - Ask Leo!
Who is Leo?
Link to Leo!

Terms, Conditions & Privacy