Script to Find Attached USB Mass Storage Devices

Share on:

I wrote the following script to find and list attached USB Mass Storage Devices and determine the disks and partitions associated with them.

 1$usbKeysAttached = Get-PnpDevice -Class USB -Status OK -FriendlyName "USB Mass Storage Device"
 2
 3$aAttachedDisks = @()
 4
 5ForEach($key in $usbKeysAttached) {
 6    $pnpProperties = $key | Get-PnpDeviceProperty
 7    $pnpBusRelations = ($pnpProperties.Where({$_.KeyName -eq "DEVPKEY_Device_BusRelations"})).Data | Select -First 1
 8    
 9    $usbDisk = Get-Disk -UniqueId "$pnpBusRelations\`:$env:COMPUTERNAME"
10
11    $usbPartitions = Get-Partition -DiskId $usbDisk.Path
12
13    If(-not($usbPartitions)) {
14        # There are no partitions, just show the disk info.
15        $oData = New-Object PSObject
16        Add-Member -InputObject $oData -MemberType NoteProperty -Name "DiskName" -Value $usbDisk.FriendlyName
17        Add-Member -InputObject $oData -MemberType NoteProperty -Name "DiskSize" -Value $usbDisk.Size
18        Add-Member -InputObject $oData -MemberType NoteProperty -Name "PnPInstanceId" -Value $key.InstanceId
19        $aAttachedDisks += $oData
20    } else {
21        ForEach($partition in $usbPartitions) {
22            $oData = New-Object PSObject
23            Add-Member -InputObject $oData -MemberType NoteProperty -Name "DiskName" -Value $usbDisk.FriendlyName
24            Add-Member -InputObject $oData -MemberType NoteProperty -Name "DiskSize" -Value $usbDisk.Size
25            Add-Member -InputObject $oData -MemberType NoteProperty -Name "PnPInstanceId" -Value $key.InstanceId
26            Add-Member -InputObject $oData -MemberType NoteProperty -Name "DriveLetter" -Value $partition.DriveLetter
27            Add-Member -InputObject $oData -MemberType NoteProperty -Name "DriveSize" -Value $partition.Size
28            $aAttachedDisks += $oData
29        }
30    }
31}
32
33ForEach($drive in $aAttachedDisks) {
34
35    If($drive.DriveSize -ge 1TB) {
36        $drive.DriveSize = "$([Math]::Round($drive.DriveSize/1TB,2)) TB"
37    } elseif ($drive.DriveSize -gt 1GB) {
38        $drive.DriveSize = "$([Math]::Round($drive.DriveSize/1GB,2)) GB"
39    } elseif ($drive.DriveSize -gt 1MB) {
40        $drive.DriveSize = "$([Math]::Round($drive.DriveSize/1MB,2)) MB"
41    } else {
42        $drive.DriveSize = "$([Math]::Round($drive.DriveSize/1MB,2)) B"
43    }
44
45    If($drive.DiskSize -ge 1TB) {
46        $drive.DiskSize = "$([Math]::Round($drive.DiskSize/1TB,2)) TB"
47    } elseif ($drive.DiskSize -gt 1GB) {
48        $drive.DiskSize = "$([Math]::Round($drive.DiskSize/1GB,2)) GB"
49    } elseif ($drive.DiskSize -gt 1MB) {
50        $drive.DiskSize = "$([Math]::Round($drive.DiskSize/1MB,2)) MB"
51    } else {
52        $drive.DiskSize = "$([Math]::Round($drive.DiskSize/1MB,2)) B"
53    }
54
55    If($drive.DriveLetter.length -eq 1) {
56        $drive.DriveLetter = "$($drive.DriveLetter):"
57    }
58}
59
60Clear-Host
61$aAttachedDisks | Sort DriveLetter | FT PnpInstanceId,DiskName,DiskSize,DriveLetter,DriveSize


No comments