How to eject the USB device from the system using PowerShell?

To eject the USB device from the system, we first need to get the USB device using PowerShell. WMI class Win32_Volume will help us to find the USB device.

We know that all removal devices using the DriveType '2'. So we will filter out the USB device among the listed devices.

PS C:\> $usbdev = gwmi win32_volume | where{$_.DriveType -eq '2'}

The below commands will helpful to unallocated the USB from the system.

PS C:\> $usbdev.DriveLetter = $null
PS C:\> $usbdev.Put()


Path : \localhost\root\cimv2:Win32_Volume.DeviceID="\\?\Volume{6e4d6f1e-a8c2-11eb-9493-005056c00008}\"
RelativePath : Win32_Volume.DeviceID="\\?\Volume{6e4d6f1e-a8c2-11eb-9493-005056c00008}\"
Server : localhost
NamespacePath : root\cimv2
ClassName : Win32_Volume
IsClass : False
IsInstance : True
IsSingleton : False

And the below command will dismount the disk from the system.

PS C:\> $usbdev.Dismount($false,$false) | Out-Null

You can find the method Dismount details for the Removal Disk is as below.