Technology in terms you understand. Sign up for the Confident Computing newsletter for weekly solutions to make your life easier. Click here and get The Ask Leo! Guide to Staying Safe on the Internet — FREE Edition as my thank you for subscribing!

Is There a Way to Safely Remove Hardware from a Batch File?

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.

Become a Patron of Ask Leo! and go ad-free!

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.

Subscribe to Confident Computing! Tech problem solving & safety tips & a weekly confidence boost in your inbox every week.

I'll see you there!

10 Reasons Your Computer is Slow

Slow Computer?

Speed up with my special report: 10 Reasons Your Computer is Slow, now updated for Windows 10.

NOW: name your own price! You decide how much to pay -- and yes, that means you can get this report completely free if you so choose. Get your copy now!

54 comments on “Is There a Way to Safely Remove Hardware from a Batch File?”

  1. I used devcon to remove a usb external hard drive, but then the device automatically reinstalls. I’m trying to find a way to just “stop” the device so that the disk doesn’t get corrupted when the users just pull its plug. I’m having a hard time finding a good solution.

    Reply
  2. I’m surprised the drive re-detects right away. I’d only expect that on a reboot, or perhaps accompanying some other device install. Not sure how to address that.

    One thing you might try to avoid corruption is “sync”, a tool that fluishes buffers to disk on NTFS drives – http://ask-leo.com/d-40527b

    Reply
  3. This question is remotely related but not specific to the info above.

    My “Safely Remove Hardware” icon (Windows XP)has disappeared and I’ve tried everything I know of to restore it to the taskbar. In the taskbar Properties I have it flagged as “Always Show” along with other icons, but it does not appear in the taskbar when using any USB device…external CD burner, camera, etc.. Don’t know what to do.

    Reply
  4. Great tool. Nice to automate the safe removal of my iPod using a simple batch file. One thing I noticed tho is that when you use the safely remove hardware in the system try and than reattatch the device, XP simply picks up the device and activates it. But when you use the remove command with devcon and than reconnect the device it has to reinstall it as new hardware again. I assume “devcon remove” totally gets rid of all the drivers that were installed with the device. Any ideas on this??

    Reply
  5. i have a windows xp compac computer the icon is in my task bar is it safe to remove it i dont know what to do i had my computer for 3 months help

    Reply
  6. Try ‘devcon disable ‘ instead – this means it will not automatically re-install. However, the device will remain disabled once you plug it back in, so you will also need a file which runs as ‘devcon enable ‘ which you will need to run when re-inserting. Still, 2 double-clicks and two icons can be a much more convenient solution!

    Reply
  7. I downloaded the devcon file but it will not run with the message “missing
    export SETUPAPIDLL.COM …”
    Is any one familiar with message and how to fix this problem.

    Reply
  8. My unplug or eject hardware icon has disappeared too. I’m not sure what to do about it or how to fix it? I’ve searched google for answers and found some, but none fix the problem. I don’t want to use some other software to unplug or eject hardware. If anyone has any solutions I would really appreciate the help. Thanks.

    Travis
    San Diego, CA.

    Reply
  9. Hi,

    This works fine, but when you have two MAXTOR ONETOUCH, the ID is almost the same, and when you that the whole ID, it has problems with the & .
    Do you know a solution for this ?

    Reply
  10. My remove safely icon disappeared also, and the system reboots when USB SD card reader is removed. (and sometimes whane a flash memory decice is removed)

    Tried that “safely Remove” and when stopping a device the Machine still reboots when even SELECTING a device (WIN XP SP 2) – got rid of THAT quick!

    Reply
  11. I’ve WindowsXP installed in my PC with whole system functioning very well. But, I generally don’t see any “safely remove your hardware” icon in the system tray when I plug any USB flash device. It appears only when I log off windows and log it on again.
    I’ve got a USB memory-card-reader attached with my system. It suggests this icon to be appered always. Though the card is still in function, it doesn’t make the icon on the tray appear always anymore, even not in case of plugging any new flash drive.

    I need your suggestion very much.

    Thank you.

    Reply
  12. For those of you who do not see the icon in their system tray, you may run this command to bring it up:

    %windir%\system32\RUNDLL32 shell32.dll,Control_RunDLL hotplug.dll

    If you want to control the behavior of the Safely Remove Hardware icon in your system tray, do the following:

    1. Right-Click on the Task Bar
    2. Select Properties
    3. Under the taskbar tab, click the Customize button
    4. Select the program you wish to hide
    5. Under the drop down box, select Always Hide, Always Show, or Hide when inactive.
    6. Click OK

    enjoy 🙂

    Reply
  13. Thanks for the string. I created a .bat file with in notepad called “Eject.bat” (adding a carriage return at the end of your string) then placed a shortcut to it in my Quick launch bar.

    Works a treat.

    Reply
  14. I did as skeetabomb but for those who doesnt know procedure

    -create new text document.txt with right click on Desktop

    -copy command from razor2000 (thanx razor2000) %windir%\system32\RUNDLL32 shell32.dll,Control_RunDLL hotplug.dll

    -paste it in to .txt file

    -save

    -rename to whatever.bat

    Thanks razor and skeetabomb

    Reply
  15. It seems a lot of people can’t find the Safely Remove icon on their taskbar! Me included. It just disappeared, and my Tosh gets very upset if I change the floppy drive for the CD drive, since I can’t “safely remove”.

    If anyone has found the answer I’d be most grateful for an email. Thanks. Harry
    harry@tifosi.net

    Reply
  16. For the Safely Remove Hardware to appear you need to:

    Right click on the drive (my instance drive I)

    Click Properties
    Click Hardware tab
    Highlight your drive
    Click properties
    Click policies tab
    Click optimize for performance.
    Click Ok.
    Click apply.

    You may have to restart your computer but thats how it worked for me.

    For some reason XP sometimes selects optimize for quick removal as an option.

    pete

    Reply
  17. The batch file sounds like a good solution & I’ll certainly try, but is there a way to find out what program is still accessing the drive?

    I have a USB Apricorn portable HDD for backups & to take files home from work & viceversa. At home (XP Home) I rarely have a problem, but at work (W2K) too frequently I’m unable to “safely remove” it unless I reboot the machine. The access light is not on & it may be some time since I’ve used it at all. Waiting a while (even hours) doesn’t help. Of the times I’ve unplugged it anyway, maybe half of them I get a warning that some process didn’t finish, no mention of exactly which or what program. Closing every program running (that I can see) is no help either.

    To date I haven’t found any of my files in the portable missing or damaged, but I need to know what’s accessing it without my authorization. We have a good firewall & virus protection, but since it’s a work computer you never know what else is there. Virus & spyware check are all ok.

    Many thanks.

    Rosie

    Reply
  18. Is there a way to do it the other way around? When I plug my iPod, iTunes is supposed to start as well, but it doesn’t. So Is there a way to trigger iTunes when I plug he iPod?

    Reply
  19. My “Safely Remove Hardware” icon (Windows XP)has disappeared and I’ve tried everything I know of to restore it to the taskbar. In the taskbar Properties I have it flagged as “Always Show” along with other icons, but it does not appear in the taskbar when using any USB device

    Reply
  20. Doesn’t work! Not for me. Looks like “devcon remove” causes the device in question to be uninstalled — which is not desired, and does NOT cause it to be “unplugged” — which -is- desired.

    In other words, after using “devcon remove” the device is completely gone from the device manager (even in “show hidden” mode) but I still get the “Unsafe Removal” error (albeit with different device syntax) when I physically unplug the device. (I may be getting the Unplug error because the related drivers — the “Generic volume” driver and the “USB Mass Storage Device” driver are still in the stack.)

    When I plug the device back in I get a “Found new hardware” sequence because the drivers had been (undesireably) uninstalled.

    And methinks that uninstalling a device may be actively dangerous — it may NOT wait to flush disk buffers, since it’s not an “unplug”. Using this removal method may cause you to lose data. I would avoid it until this is clarified.

    I tested with two different plug-in drives, under Win2000 SP4. I have no idea why the method apparently does work for Leo!

    Reply
  21. Found one that works better! It seems that, in Windows terms, you want an “Eject”, not a “Remove”, of the device.

    To do that, grab ‘deveject’ from ftp://ftp.heise.de/pub/ct/listings/0316-208.zip. To “safely remove” drive letter ‘X:’, the syntax would be “deveject -EjectDrive:X:”. This method does NOT uninstall the whole device, does NOT truncate writes-in-progress, does NOT lose the drive lettering (if you’ve set it), DOES really get rid of the “Unsafe Removal” messages. Good.

    And for extra credit, “unmount.exe” from http://www.uwe-sieber.de/files/unmnt10.zip will wait for open files to close. Syntax is “unmount -e X:”. Run it first, then do the deveject. (There is an “eject” built into unmount.exe, but it’s not the right one.)

    Reply
  22. I’m agree with Tintin, DevEject seems to be better solution. No installation is needed, so we can just copy it and write .bat file – something like this: deveject -EjectId:usbstor\*. We can also make shortcut to DevEject and add the parameter to its properties (something like this: [“C:\Documents and Settings\Admin\deveject.exe” -EjectId:usbstor\*] ).

    A bit of problem – if drive is in use and cannot be ejected, DevEject will not in fact eject it, but will keep silence on it (report it’s done). If you have not the icon in your deskbar, you probably will not notice it and will have true chance to corrupt your data.

    Reply
  23. Is it possible to put the ‘disconnect’ command (any of the above methods) in a .bat file that is on the drive that I am trying to remove.

    Reply
  24. I don’t see why not. The batch file would terminate abruptly, since it would “disappear” while it was running.

    Alternative is to have the batch file copy itself to the %TMP% location, and then run itself from there, deleting itself on completion.

    Reply
  25. Is there a way to stop the usb drive to safely remove it? At college, I frequently use computers on which I have limited access, and if the “safely remove” icon is not there, I cannot adjust the settings on the computer. I have to completely shut down the computer to stop the drive. Also, if the drive is “optimized for quick removal”, does the drive need to be stopped before it is removed? The light on the drive stays on until I pull it out (that is, if I don’t stop it before removing it), which seems to me like a bad thing.

    Reply
  26. I seem to have the opposite problem. I have a digital camcorder which I have previously connected (successfully) via USB port. I then installed an external hard drive backup. Now when I plug in the camcorder, Windows just comes up with teh ‘safely remove hardward’ icon & seems to think that the camcorder is an external disc drive. The camcorder software seems to think that no device is connected. HELP!

    Reply
  27. After reading about the misadventures of using Devcon, I thought you might like to how I do this. This is a batch file of the safely remove dll.
    Note that this may not work on restricted acess machines.

    Open notepad and copy the lines between the *’s (do not include the *’s)

    *******************************

    REM SAFEREMOVE.BAT

    echo off
    echo SAFELY REMOVE
    echo Starting Safely Remove
    %windir%\system32\RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll

    ******************************************

    Save the file as Safely_Remove.bat
    Make sure that you have the .bat added to the file name.

    You can copy this file to your USB drive, and then take it with you.

    I recommend copying it to the desktop of the computer that you are on before trying to use the batch file, as the drive can’t be stopped while running the batch file.

    If this doesn’t work (i.e. very restricted access, I run into this at school where the system directory is off limits – perhaps rightly so…. but I digress) you can copy the 3 files that the batch file uses from your home computer. Just put them in a folder, copy your shortcut to “safely remove”, and change the shortcut properties accordingly. If you don’t know how to do that, I can post it later.

    There is also a program called Safely Remove. You can download a free 30-day(?) trial. Ironically, if you copy the executable file to your USB drive, you can take it with you, copy it to the computer that you are using, and it will work. Not only that, but by having it on the USB drive, the trial period never runs out, that is, it says “day one” each time you use it. Very cool, although unethical!!

    Reply
  28. I found an amazing program that allows you to quickly remove the USB devices that you commonly use called RemoveDrive. I found it when I was researching some of the other program alternatives like “USB Safely Remove” (which is shareware and has an annoying popup screen) & “DevEject” (Which is another command line excecutable like RemoveDrive, but it does not remove drives considered as “Local Drives”).

    RemoveDrive is a command line executable, so you would need to tell it what to remove drive by drive. I saved the RemoveDrive.exe to “C:\Program Files\RemoveDrive” and then created a BAT Batch file with the following:

    ————————————————–
    CD\
    CD C:\Program Files\RemoveDrive
    RemoveDrive.exe “Simple Flash Disk 2.0 USB Device”
    RemoveDrive.exe “Multi Flash Reader USB Device”
    ————————————————–

    The device names in quotes are the names of my USB devices as they appear in the “Safely Remove Hardware” >> “Stop a Hardware device” screen that you would normally have to use to remove your devices. Now I have the Batch file assigned to a “Hot Key Plus” combination, and I can remove my USB drives in a flash!

    You can get the RemoveDrive.exe file at: [url]http://www.uwe-sieber.de/usbstick_e.html[/url]

    Hope this helps

    Reply
  29. 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

    Reply
  30. 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.

    Reply
  31. 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.

    Reply
  32. 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

    Reply
  33. —–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—–

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

    Reply
  35. If you are using this in window 7 home premium, then you will need to open the command prompt as admin. Otherwise when you run the file it will open in a new window (at least it did on my machine).

    Reply

Leave a reply:

Before commenting please:

  • Read the article.
  • Comment on the article.
  • No personal information.
  • No spam.

Comments violating those rules will be removed. Comments that don't add value will be removed, including off-topic or content-free comments, or comments that look even a little bit like spam. All comments containing links and certain keywords will be moderated before publication.

I want comments to be valuable for everyone, including those who come later and take the time to read.