private/Disk/Get-Win32DiskDriveUsb.ps1
|
function Get-Win32DiskDriveUsb { <# .SYNOPSIS Returns physical disks connected by USB .DESCRIPTION Queries the Win32_DiskDrive WMI class through the .NET System.Management API and returns each installed external physical disk. USB disks can be exposed through the SCSI driver stack, so InterfaceType cannot reliably identify them. This function does not require the Storage PowerShell module. .OUTPUTS System.Management.ManagementObject Returns a Win32_DiskDrive object for each USB-connected physical disk. .EXAMPLE PS> Get-Win32DiskDriveUsb Returns all USB-connected physical disks installed on the local computer. .NOTES Author: David Segura Company: Recast Software Dependencies: .NET Classes: [System.Management.ManagementObjectSearcher] #> [CmdletBinding()] [OutputType([System.Management.ManagementObject])] param () Write-Verbose "[$(Get-Date -Format s)] [$($MyInvocation.MyCommand.Name)] Start" try { $searcher = [System.Management.ManagementObjectSearcher]::new( 'root\CIMV2', "SELECT * FROM Win32_DiskDrive WHERE MediaType = 'External hard disk media' AND MediaLoaded = TRUE AND Size > 2147483648" ) foreach ($disk in $searcher.Get()) { $disk } } catch { Write-Error -Message "[$(Get-Date -Format s)] [$($MyInvocation.MyCommand.Name)] Unable to enumerate USB disks. $($_.Exception.Message)" -ErrorAction Continue } finally { if ($null -ne $searcher) { $searcher.Dispose() } } Write-Verbose "[$(Get-Date -Format s)] [$($MyInvocation.MyCommand.Name)] End" } |