"safe to remove Hardware" message

Powershell module to Eject removable drives

Just made a little Powershell module to allow for easy USB drive ejecting from the command line. Itch: scratched.

Use it without a drive letter and it will list all your removable drives:

Available removable drives:

Use it with a drive letter and it ejects the specified drive:

Eject-USB J

To install add this line to your powershell profile (in your Documents folder\Windows Powershell\profile.ps1)

Import-Module "path\to\the\script\ejectUSB.ps1"

Source code below, or download it here

Function Eject-USB{
  param (
  $vols = get-wmiobject -Class Win32_Volume -filter Drivetype=2| %{$_.DriveLetter}
  if ($letter -eq ""){
    # no drive letter given, just list the removable disks
    if ($vols.length -gt 0){
      echo "Available removable drives:"
      echo $vols
    } else {
      echo "No removable drives present"
  } else {
    if ($vols -contains ($letter + ":")){
      $driveEject = New-Object -comObject Shell.Application
      $driveEject.Namespace(17).ParseName("{0}:" -f $letter).InvokeVerb("Eject")
    } else {
      echo ("drive {0} not mounted, or not removable" -f $letter)

Leave a Reply

Your email address will not be published. Required fields are marked *