Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommymaynard/786e1d8e3f897a7aaef867814a21529d to your computer and use it in GitHub Desktop.
Save tommymaynard/786e1d8e3f897a7aaef867814a21529d to your computer and use it in GitHub Desktop.
Map Drive to Drive Letter Using the Win32_DiskDrive Interface Type Property
<#
TechNet Contribution: Map Drive to Drive Letter Using the Win32_DiskDrive Interface Type Property
Previous link: https://gallery.technet.microsoft.com/Map-Drive-to-Drive-Letter-1fff91ad
Downloaded: 2,904 times (as of 05/21/2020)
This project began on the TechNet forums. Someone requested help determining the drive letter of a USB drive.
It was first recommended to use the Win32_LogicalDisk class, but I was concerned that this might return false
results since an SD card would report the same Description and DriveType as a USB using Win32_LogicalDisk.
I recommended using the Win32_DiskDrive class and the InterfaceType property so the user could ensure the drive
was a USB drive. Unfortunately, the Win32_DiskDrive class does not include a drive letter property and so I put
some code together and shared it on the forum. Since then, it has been updated as an advanced function.
The Get-TMDriveToLetter advanced function allows a user to get all the drives on their system, with or without
specifying the interface type, and map those to their respective drive letters. Interface types include SCSI, HDC,
IDE, USB, and 1394. This advanced function is untested on Windows Vista, XP, and 2000.
Updated to version 2.1 on 7/31/2014
Updated to version 2.2 on 6/19/2015
Now includes Firmware Revision when using the -ShowMore parameter (image below has not been updated).
#>
Function Get-TMDriveToLetter {
##### ** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK **
<#
.SYNOPSIS
This advanced function returns the local computer drive(s) by interface type and displays the matching drive letter.
.DESCRIPTION
Without parameters, Get-TMDriveToLetter returns all of a local computer drive(s) by interface type and displays the matching drive letter. Using the -Type parameter allows an interface type, such as USB, to be specified. The -ShowMore parameter will also include the caption, disk partition, and device ID.
.PARAMETER Type
This parameter allows an interface type to be specifed. The interface types are SCSI, HDC, IDE, USB, and 1394.
.PARAMETER ShowMore
This parameter will include the caption, disk partition, and device ID.
.EXAMPLE
PS C:\>Get-TMDriveToLetter
DriveLetter InterfaceType
----------- -------------
C: SCSI
F: SCSI
G: USB
.EXAMPLE
PS C:\>Get-TMDriveToLetter -Type USB
DriveLetter InterfaceType
----------- -------------
G: USB
.EXAMPLE
PS C:\> Get-TMDriveToLetter -Type usb -ShowMore
Caption : LaCie CooKey USB Device
InterfaceType : USB
DiskPartition : Disk #2, Partition #0
DeviceID : \\.\PHYSICALDRIVE2
DriveLetter : G:
.EXAMPLE
PS C:\> Get-TMDriveToLetter -Type ide
INFO: No matching drives found (IDE)
.EXAMPLE
PS C:\> Get-TMDriveToLetter -Type xyz -ShowMore
WARNING: No Interface Type Match for (XYZ)
.NOTES
NAME: Get-TMDriveToLetter
AUTHOR: Tommy Maynard
VERSION: 2.1
Additions 2.0:
-Added names for positional parameters
-Added ShowMore parameter (and modified default output: only drive letter)
-Added Interface Type to results
-Improved results' spacing
-Improved comment-based help
Additions 2.1:
-Stored data in custom object
-Modifed default output to include interface type (due to using custom object)
Additions 2.2:
-Added Firmware Revision when -ShowMore parameter is used.
LASTEDIT: 06/19/2015
#>
[CmdletBinding()]
Param(
[string]$Type,
[switch]$ShowMore
)
Begin {
switch ($Type) {
'SCSI' {$DiskDrives = Get-WmiObject -Class Win32_DiskDrive | Where-Object {$_.InterfaceType -eq 'SCSI'} | Select-Object -Property Caption,DeviceID,InterfaceType,FirmwareRevision}
'HDC' {$DiskDrives = Get-WmiObject -Class Win32_DiskDrive | Where-Object {$_.InterfaceType -eq 'HDC'} | Select-Object -Property Caption,DeviceID,InterfaceType,FirmwareRevision}
'IDE' {$DiskDrives = Get-WmiObject -Class Win32_DiskDrive | Where-Object {$_.InterfaceType -eq 'IDE'} | Select-Object -Property Caption,DeviceID,InterfaceType,FirmwareRevision}
'USB' {$DiskDrives = Get-WmiObject -Class Win32_DiskDrive | Where-Object {$_.InterfaceType -eq 'USB'} | Select-Object -Property Caption,DeviceID,InterfaceType,FirmwareRevision}
'1394' {$DiskDrives = Get-WmiObject -Class Win32_DiskDrive | Where-Object {$_.InterfaceType -eq '1394'} | Select-Object -Property Caption,DeviceID,InterfaceType,FirmwareRevision}
'*' {$DiskDrives = Get-WmiObject -Class Win32_DiskDrive | Select-Object -Property Caption,DeviceID,InterfaceType}
default {
If ($Type -gt 0) {
Write-Warning -Message "No Interface Type Match for ($($Type.ToUpper()))"
$SkipProcess = $True
} Else {
$DiskDrives = Get-WmiObject -Class Win32_DiskDrive | Select-Object -Property Caption,DeviceID,InterfaceType,FirmwareRevision
}
}
}
} # End Begin
Process {
If ($SkipProcess -ne $True) {
If ($DiskDrives) {
$Object = @()
foreach ($DiskDrive in $DiskDrives) {
$Query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + $DiskDrive.DeviceID + "'} where AssocClass = Win32_DiskDriveToDiskPartition"
$DiskPartitions = Get-WmiObject -Query $Query
foreach ($DiskPartition in $DiskPartitions) {
$Query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + $DiskPartition.DeviceID + "'} where AssocClass = Win32_LogicalDiskToPartition"
$LogicalDisks = Get-WmiObject -Query $Query
foreach ($LogicalDisk in $LogicalDisks) {
If ($ShowMore) {
$Object += New-Object PSObject -Property @{
Caption = $DiskDrive.Caption
DeviceID = $DiskDrive.DeviceID
DiskPartition = $DiskPartition.DeviceID
DriveLetter = $LogicalDisk.DeviceID
InterfaceType = $DiskDrive.InterfaceType
Firmware = $DiskDrive.FirmwareRevision
}
} Else {
$Object += New-Object PSObject -Property @{
DriveLetter = $LogicalDisk.DeviceID
InterfaceType = $DiskDrive.InterfaceType
}
}
}
}
}
} Else {
Write-Output -Verbose "INFO: No matching drives found ($($Type.ToUpper()))"
}
}
Write-Output -Verbose $Object
} # End Process
} # End Function
@tommymaynard
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment