13

I am attempting to dismount an external USB drive using powershell and I cannot successfully do this. The following script is what I use:

#get the Win32Volume object representing the volume I wish to eject 

$drive = Get-WmiObject Win32_Volume -filter "DriveLetter = 'F:'"

#call dismount on that object there by ejecting drive

$drive.Dismount($Force , $Permanent)

I then check my computer to check if drive is unmounted but it is not.

The Boolean parameters $force and $permanent have been tried with different permutations to no avail. The exit code returned by the dismount command changes when the params are toggled.

(0,0) = exit code 0

(0,1) = exit code 2

(1,0) = exit code 0

(1,1) = exit code 2

The documentation for exit code 2 indicates that there are existing mount points as a reason why it cannot dismount. Although I am trying to dismount the only mount point that exists so I am unsure what this exit code is trying to tell me.

Having already trawled the web for people experiencing similar problems I have only found one additional command to try and that is the following:

# executed after the .Dismount() command

$drive.Put() 

This additional command does not help.

I am running out of things to try, so any assistance anyone can give me would be greatly appreciated.

Ploni
  • 103
  • 6
JB.
  • 233
  • 1
  • 2
  • 6
  • You probably don't want to dismount a usb drive. You'll have to reassign the drive letter in disk management later. – js2010 Sep 12 '18 at 14:39

3 Answers3

14

A clean solution that doesn't leave any half baked open references like the sample from pk does is:

  $driveEject = New-Object -comObject Shell.Application
  $driveEject.Namespace(17).ParseName("E:").InvokeVerb("Eject")

Retrieved from http://sushihangover.blogspot.nl/2012/02/powershell-eject-local-or-remote.html

Koen Zomers
  • 281
  • 2
  • 4
  • My previously posted solution was indeed correct. I'm a bit uncertain as to how I ever offered it up! Please use this method. – pk. Mar 06 '14 at 21:54
  • 1
    Note that this can be made run as a single line from the Run dialog: ```powershell (New-Object -comObject Shell.Application).Namespace(17).ParseName(\"E:\").InvokeVerb(\"Eject\")``` – Deebster May 07 '17 at 13:57
  • 1
    That one-liner works but without the backslashes. – js2010 Sep 12 '18 at 14:42
  • 1
    Unfortuntatly, that does not work here. I also don't get any error message or any error message. `$?` is `True` after invoking the second line. Is there anything I can do? – René Nyffenegger Sep 22 '19 at 21:49
  • 1
    @RenéNyffenegger, I think if you are using a non-English version of Windows you'll have to use the localized version of `Eject` in your language. That's common with pretty much all Shell COM functions. There are some exceptions, like the standard "Open" and "Print" verbs. – efotinis May 24 '20 at 18:46
  • @Deebster: doesn't work for me on Win 10; even the 2 commands separated by ";" doesn't work either from cmd. Only works from Powershell. efotinis: Rene's command doesn't return any errors and doesn't work could be due to powershell exiting before command completes. – Zimba Feb 05 '21 at 08:43
  • @RenéNyffenegger: add delay after command to enable execution: `powershell "$driveEject = New-Object -comObject Shell.Application; $driveEject.Namespace(17).ParseName(\"E:\").InvokeVerb(\"Eject\"); start-sleep -s 3"` – Zimba Feb 05 '21 at 08:44
2

I cannot do a great job answering from the PowerShell end, by you might want to look at how mountvol operates. There are different degrees of dismount.

Mountvol or Mountvol /?

Displays the name, globally unique identifier (GUID), and location of the volume.

Mountvol [drive:]path VolumeName

Creates a new volume mount point. Specify either a drive letter root directory or an existing empty NTFS directory as the source of the mount point and a volume name as the target.

Mountvol [drive:]path /D

Deletes an existing volume mount point.

Mountvol [drive:]path /L

Lists a volume name for a given volume mount point.

I tried doing something similar to what you required in VBScript to reorder devices on old desktops where drive mounts for older hardware that was too tedious to remove (read Flash drive) was interfering with shortcuts on a custom GUI where I could only explicit mention mountpoints (D:, E:, F:) with a particular purpose. This became superbly frustrating, and you have to unmount and remount things using mountvol inputs and outputs to verify it went well and proceed with changes. I hope you have better luck than me.

songei2f
  • 1,924
  • 1
  • 20
  • 30
0

Powershell, like CMD, can call Win COM Objects which have the needed functions to mount or unmount disk drives eg. Shell.Application.

In powershell eg. via cmd:

powershell "$driveEject = New-Object -comObject Shell.Application; $driveEject.Namespace(17).ParseName(\"E:\").InvokeVerb(\"Eject\"); start-sleep -s 3"

Ejecting disk via powershell also requires it to be unplugged & plugged back in to be mounted again. Otherwise the disk can just be unmounted so it's inaccessible until it's mounted again by either mountvol or diskpart.

eg. with mountvol:

REM unmount
mountvol e: /D or /P
REM mount for access
mountvol e: \\?\Volume{device GUID in hex with hyphens}\

mountvol allows mounting devices to a folder eg. C:\fakedisk\ as well as a drive letter.

eg. With diskpart:

REM unmount
(
echo select volume 3
echo remove letter=e dismount
) | diskpart

REM mount again for access
(
echo select volume 3
echo assign letter=e
) | diskpart

Tested in Win 10 cmd


Zimba
  • 101
  • 1