Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm constructing a backup script for Windows 7, and the last action I want it to perform is to safely "remove" the USB drive that it is backing up to. I am under the impression that plugging the drive into the SAME USB port all the time will keep the same DEV_ID, correct me if I'm wrong. With a Command Line (or PowerShell), how can I tell Windows to safely remove the hardware automatically without user input?

Just as a place holder, other OSes that may have a way to do this would be great to know as well

share|improve this question
    
since other OSes have a more established commandline, it shouldn't be too hard to figure out –  stijn Jun 29 '12 at 20:12
1  
I have to admit disappointment by some of the “answers” below which clearly did not understand what is being asked and provided options that are obviously useless for the task of automation as asked.` ಠ_ಠ –  Synetech Dec 5 '13 at 3:39
    
> I am under the impression that plugging the drive into the SAME USB port all the time will keep the same DEV_ID, correct me if I'm wrong. That is correct. Windows treats a drive and the port it’s plugged into as an identifying pair. By plugging it into a different port (in which it has never been plugged), you will trigger Windows’ driver-installation function. Worse, if you have assigned a non-consecutive drive-letter to the volume, then plugging it into a new port will not retain that letter, and it will get the next available letter and you must assign a letter manually again. –  Synetech Dec 5 '13 at 3:44
    
(Oddly enough, if you plug a drive into a port and let Windows install drivers for it, then assign a non-consecutive drive-letter to it, then unplug it an plug in a different drive that is of the same make, model, and size, then Windows will treat it exactly as though you plugged in the previous drive again.) –  Synetech Dec 5 '13 at 3:45
add comment

4 Answers

up vote 10 down vote accepted

RemoveDrive has served me well in the past

share|improve this answer
    
That's exactly what I need! –  Canadian Luke Jun 29 '12 at 20:17
add comment

Besides Uwe Sieber's RemoveDrive mentioned in the other answer, there are a whole bunch of utilities that can accomplish this. A small list follows:

  • USB Disk Ejector is primarily a GUI-based utility but can be used equally well from the command-line to eject the drive that the program is running from, or any drive by specifying the drive letter / (partial) drive name / mountpoint etc. Free and open source.

    USB Disk Ejector

  • USB Safely Remove is not free but it's a disk removal utility on steroids, with lots of advanced features, including of course command-line support. Zentimo is its bigger brother, with even more features.

    USB Safely Remove

  • Microsoft's own DevCon is the command-line version of Device Manager. Besides the original Win2K/XP-era version available from the KB page, there are newer releases (both 32 and 64-bit) available from various MS sources as mentioned in this Where to find DevCon.exe article. DevCon.exe for Windows 7 (and probably Windows 8 as well) can be found buried in the appropriate Windows Driver Kit (WDK), as mentioned in this thread (which also contains download links to the extracted executable).

    devcon status * or devcon hwids * or devcon findall =usb (for a more compact listing) should tell you the hardware ID of the device. For example:

    USB\VID_0781&PID_7113\0001162825
    Name: USB Mass Storage Device
    Driver is running.

    You can then try removing the device with devcon remove "USB\VID_0781&PID_7113" (wildcards like * are allowed, but be careful or you might end up removing something else entirely!)


Someone asked "Is there a DOS prompt (cmd.exe from Win7) command to eject a thumb drive?" which was unfortunately closed as a duplicate of this thread. However, the question was about ejecting USB drives while in the Windows Recovery Console / System Recovery Command Prompt, so it is unlikely any of the utilities above will help. In such a situation, the following method using Diskpart should work:

  1. Type diskpart and wait for the diskpart prompt (DISKPART>)

  2. Type list volume

  3. Note the volume number of the USB drive carefully (use listed properties such as drive letter, label, type and size for help)

  4. Type select volume <number>, where <number> is the volume number noted above

    Diskpart

  5. Type remove all dismount

  6. Type exit to quit Diskpart

Now you should be able to safely remove your USB drive without fear of data loss.

share|improve this answer
    
Thanks for taking time to respond to my question even after SU closed it. I went ahead and acted on Jared Tritsch's suggestion and removed the drive just by removing it. Once I get laptop operational again, I'll experiment with with DISKPART. Here's the link to Microsoft's Specs for DISKPART. technet.microsoft.com/en-us/library/bb490893.aspx –  zundarz Oct 12 '12 at 3:19
    
@zundarz: You're welcome! The default Removal policy in Windows for removable devices is Quick removal, which disabled write caching and allows one to remove the drive without needing to safely remove it first. See this screenshot for more. Thus Jared's suggestion about yanking the drive shouldn't ordinarily cause data loss if removable drives are treated the same way in the Recovery Console as well. Still, for peace of mind I always take care to safely remove each drive before pulling it out. Might be a placebo, but keeps me from worrying. :) –  Karan Oct 12 '12 at 4:28
    
P.S. Be careful with Diskpart - it's powerful and hence quite dangerous. The reason I used it for drive dismounting is because it's available from both XP's Recovery Console and Vista/7's System Recovery Options / Command Prompt. –  Karan Oct 12 '12 at 4:38
    
I tried using Diskpart while in normal Windows mode. This is the message I get after performing step 5 above. "DiskPart successfully removed the drive letter, but your computer needs to be rebooted before the changes take effect. Diskpart successfully dismounted the volume." I tried using it on a removable media (SD card) rather than a removable device (such as the SD card reader or USB flash drive). The LED indicator on the reader didn't go off but it stayed lit (i.e. it stopped flashing as it occasionally does when a SD card only sits in it). So I guess it's safe to remove then. –  sammyg May 25 '13 at 18:04
    
My SD card reader shows up as "Removable Disk" in the Computer view (Explorer). So I guess what Diskpart did is it tried dismounting the reader itself, rather than the SD card inside it. I haven't tried it with an external disk drive. It might be different story there. But generally I think the reboot message from Diskpart can be ignored. –  sammyg May 25 '13 at 18:07
show 4 more comments

With a Command Line (or PowerShell), how can I tell Windows to safely remove the hardware automatically without user input?

To answer to this question... You don't need third party stuff.

Run this command: RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll

share|improve this answer
1  
Can you describe what that does? –  Canadian Luke Oct 12 '12 at 6:19
    
launch the safe removal for usb –  climenole Oct 12 '12 at 13:53
3  
Won't I still need to click something then? –  Canadian Luke Oct 12 '12 at 14:54
    
@Luke I just tried that, it brings up the Safely Remove Hardware dialog. So yes, you would still need to select the device, click Stop button, and then Close to close the dialog box. Note that the command is case sensitive, so you have to type it exactly like it says or you will get an error from RunDLL. –  sammyg May 25 '13 at 17:43
1  
This is pretty cool, but unfortunately for automation purposes, it is no better than any of Karan’s suggestions. –  Synetech Dec 5 '13 at 3:28
add comment

According to this article, you can enable safe unplugging by setting "Quick removal". This will disable write caching per device, of which the performance impact is "negligible"?

Their steps for Windows 7:

  • plug in the device in the USB drive
  • open Device Manager
  • expand Disk Drives
  • right-click your removable drive, e.g. "USB2.0 Flash Disk USB Device."
  • select Properties
  • click the Policies tab
  • enable "Quick removal" (disable "Better performance")
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.