9

I would like to eject/dismount an external USB drive via PowerShell.

Meaning, I want to replicate exactly the behaviour of these UI actions (Ejecting drive via Windows UI):

Ejecting drive via Windows UI

Currently I am using this script:

$drive_letter="G"
$vol = get-wmiobject -Class Win32_Volume | where{$_.Name -eq $drive_letter+":\"}  
$vol.DriveLetter = $null
$vol.Put()  
$vol.Dismount($false, $false)

This works, however, when I connect the drive again, it won't show up on the list of the drives. I assume this is because of the fact that drive letter it tied to GUID of the drive and because of this: $vol.DriveLetter = $null.

I have also tried this:

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

but it didn't work.

For what it's worth, $driveEject.Namespace(17).ParseName("G:") returns:

Application  : System.__ComObject
Parent       : System.__ComObject
Name         : Backup (G:)
Path         : G:\
GetLink      :
GetFolder    : System.__ComObject
IsLink       : False
IsFolder     : True
IsFileSystem : True
IsBrowsable  : False
ModifyDate   : 12/30/1899 12:00:00 AM
Size         : 0
Type         : Local Disk

How can I dismount the drive so that it performs exactly the actions the user performs when ejecting the drive via Windows UI?

0

4 Answers 4

5

The cleanest solution would be to invoke the Eject verb, which takes care of everything.

$driveEject = New-Object -comObject Shell.Application
$driveEject.Namespace(17).ParseName("E:").InvokeVerb("Eject")
3
  • This didn't work. I've updated my question with more information.
    – user73235
    Commented Oct 31, 2022 at 20:49
  • @postanote :: psst, wrong post, he wouldn't see this in the state it is now. You would have to either explicitly @-mention him or copy/cut the comment to the root question Commented Nov 2, 2022 at 5:08
  • @user73235. Your code is dismounting the USB drive, thus forcing you to diskmgmt.msc to add it back. As for 'Savchenko Dmitriy's' code. What do you mean by 'This didn't work.'. It does eject the USB as expected, and plugging it back in brings the drive back. . If you mean the Windows Explorer UI is not auto-updated, then that is a different matter. When you do this via the taskbar, other OS code is firing via the OS, that is specific to the OS abilities. This is not native to PS or any language. You have to find all the resources involved to replicate what you are trying to do.
    – postanote
    Commented Nov 2, 2022 at 15:58
1

There are tools to do this as well.

Microsoft SysInternals

https://learn.microsoft.com/en-us/sysinternals/downloads/sysinternals-suite

Specifically

https://learn.microsoft.com/en-us/sysinternals/downloads/sync

sync.exe -accepteula -e x:

Yet. all-in-all, the above is doing what '@Savchenko Dmitriy', has already given you.

1

I had a similar issue. I fixed it by adding a slash after the drive letter G:\ instead of G:

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

$driveEject.Namespace(17) will work on normal SD Cards and USB Flash drives.

Might not work with external HDD/SSD as they are treated as SCSI devices. It does not work for me also.

I am also looking for solution to this (SCSI/USB HDD eject).

Nearest option I saw is to spawn the Safely Remove dialog via rundll, which I would prefer not to use (it has also pitfalls) if other option is available.

Edit: following might work for non ejectable USB/SCSI HDDs

$hddDevs = Get-CimInstance -ClassName Win32_DiskDrive
$externalDisks = $hddDevs | Where {$_.MediaType -like "Removable*"} | select PNPDeviceID
Disable-PnPDevice -InstanceId $externalDisks.PNPDeviceID -Confirm:$false
Enable-PnPDevice  -InstanceId $externalDisks.PNPDeviceID -Confirm:$false
1
  • sorry, but according to my tests it should be removable and not the suggested external, which is good point- * is required for like, but the value was different.
    – Gergo
    Commented Jul 4, 2023 at 17:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .