List all removable drives in PowerShell

60 views Asked by At

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?

1

There are 1 answers

1
Mostafizur Rahman On BEST ANSWER

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