Helping people with computers... one answer at a time.

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:


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.

Article C1883 - January 18, 2004 « »

Share this article with your friends:

Share this article on Facebook Tweet this article Email a link to this article
Leo Leo A. Notenboom has been playing with computers since he was required to take a programming class in 1976. An 18 year career as a programmer at Microsoft soon followed. After "retiring" in 2001, Leo started Ask Leo! in 2003 as a place for answers to common computer and technical questions. More about Leo.

Not what you needed?

May 27, 2004 9:53 AM

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.

May 27, 2004 6:58 PM

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 -

Dave Paquin
November 16, 2004 6:10 AM

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.

November 18, 2004 5:14 PM

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??

November 26, 2004 7:38 AM

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

Koina Donner
March 31, 2005 4:40 AM

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!

April 14, 2005 2:20 AM

You may use "Device Eject" ( - a software from the german computer magazine c't - Soft-Link 0316208 ( - sourcecode is included. It can be used for usb and firewire devices.

May 5, 2005 12:33 PM

Try this utility:

Aditya Mishra
May 30, 2005 4:06 PM

I downloaded the devcon file but it will not run with the message "missing
Is any one familiar with message and how to fix this problem.

August 3, 2005 11:56 AM

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.

San Diego, CA.

Stijn Smets
August 10, 2005 1:33 AM


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 ?

August 12, 2005 7:26 PM

You should be able to enclose the ID in quotes.

September 16, 2005 4:43 PM

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!

September 21, 2005 9:04 PM

this works well!
i love it...

for me .. creating a batch file.. with this.. seems to be the best way to remove thumb drives..

devcon remove usb\vid*

Echo zi
December 7, 2005 10:26 PM

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.

December 12, 2005 8:02 AM

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 :)

January 3, 2006 6:28 AM

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.

Gasper Loborec
January 6, 2006 4:51 AM

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


-rename to whatever.bat

Thanks razor and skeetabomb

January 18, 2006 12:37 PM

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

January 30, 2006 2:17 AM

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.


February 1, 2006 11:10 AM

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.


February 1, 2006 12:18 PM

Treat the drive letter ("e:" for example) as if it were a file name and search for it using the intructions with this article:

February 7, 2006 6:26 PM

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?

February 20, 2006 12:52 AM

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

Garry W
February 28, 2006 8:51 PM

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!

Garry W
February 28, 2006 9:55 PM

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

May 1, 2006 11:36 AM

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.

June 19, 2006 9:50 AM

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.

Leo A. Notenboom
June 19, 2006 3:58 PM

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.

September 7, 2006 6:58 PM

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.

September 7, 2006 7:00 PM

In the first sentence of the previous post, I meant to ask if there is a way to stop the usb drive from the command prompt.

September 14, 2006 5:31 PM

I found the answer to my question, if anyone else is looking for the same type of thing, go to : This tells you how to create a shortcut to the "safely remove" dialogue box. I copied the shortcut onto my USB drive, that way I always have the ability to stop the drive, regardless of the computer I am using.

October 16, 2006 4:56 PM

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!

November 9, 2006 3:55 AM

I downloaded the Devon program
got two programs, one don't work.
explanations here are lousey. This page is some kind of sick trick.

December 17, 2006 7:22 AM
RemoveDrive from that page works admirably.
For example:
> removedrive e:
That's it. Same effect as using the WXP Device manager :P

January 20, 2007 5:41 PM

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)



echo off
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!!

April 9, 2007 11:25 AM

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 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][/url]

Hope this helps

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

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,

May 24, 2007 1:48 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!

May 26, 2007 12:48 AM

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.

August 25, 2007 11:50 PM

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.

September 18, 2007 7:28 AM

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

October 9, 2007 1:08 AM

Worked like a charm! Excellent article.

November 28, 2007 11:37 PM

now able to see that unplag or eject hardware,

not showing usb device

able to see in mycomputer

May 26, 2008 8:11 AM

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

May 26, 2008 10:04 AM

Hash: SHA1

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


Version: GnuPG v1.4.7 (MingW32)


September 10, 2008 7:01 AM

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


April 10, 2009 1:23 AM

Can it be used to remove bluetooth devices?

October 17, 2009 1:26 PM

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

December 10, 2009 8:58 AM

hello sir,how to safely remove pen drive throug JAVA or C Lang program...

January 9, 2010 10:31 PM

it is more simple that all the above comments...
just remove the maxtor icon and the autorun.inf and u r done...

September 19, 2010 2:56 PM

devcon is an excellent command to remove all of the 6to4 interfaces, thanks!

December 2, 2010 7:43 AM

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

March 24, 2011 10:19 AM

Its a good way, but instead of ejecting the stick it uninstalls it's drivers. Thanks.

Comments on this entry are closed.

If you have a question, start by using the search box up at the top of the page - there's a very good chance that your question has already been answered on Ask Leo!.

If you don't find your answer, head out to to ask your question.