powershellwhere-object

List all removable drives in PowerShell


I am trying to get a list of all removable drives on the PC. I have a USB stick plugged in to test this.

I tried

$listDrives = Get-Volume | Select -Property DriveLetter, DriveType |
Where-Object {$_.DriveLetter -ne "`0" -and $_.DriveType -eq '2'}
Write-Output $listDrives

but it didn't output anything. Where is my mistake?


Solution

  • The PowerShell command you provided seems mostly correct for listing removable drives. However, there is a small mistake in your code. The DriveType for removable drives is not '2'; it is 'Removable'. You should update your code to filter by DriveType 'Removable' instead of '2'. Here's the corrected code:

    $listDrives = Get-Volume | Select-Object -Property DriveLetter, DriveType |
    Where-Object {$_.DriveLetter -ne $null -and $_.DriveType -eq 'Removable'}
    Write-Output $listDrives