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