Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading