private/Disk/Get-OSDeployVolumeUSB.ps1
|
function Get-OSDeployVolumeUSB { <# .SYNOPSIS Returns all volumes that reside on USB-connected disks. .DESCRIPTION Enumerates USB disks using Get-USBDisk, collects their partition access paths, and returns the matching volumes with drive letter, label, size, and health properties. An optional -FileSystemLabel parameter narrows results to volumes whose label matches the supplied value. .PARAMETER FileSystemLabel Optional. When specified, only volumes whose FileSystemLabel matches this value are returned. .OUTPUTS Selected.Microsoft.Management.Infrastructure.CimInstance .EXAMPLE Get-OSDeployVolumeUSB .EXAMPLE Get-OSDeployVolumeUSB -FileSystemLabel 'OSDEPLOY' .NOTES Author: David Segura Company: Recast Software Dependencies: Module Functions: Get-USBDisk #> [CmdletBinding()] param ( [string]$FileSystemLabel ) # Get-USBDisk filters out offline disks and card readers with no media $UsbDisks = Get-USBDisk if (-not $UsbDisks) { return } # Collect all access paths from USB partitions; skip partitions with no mount point $UsbAccessPaths = Get-Partition | Where-Object { $_.DiskNumber -in $UsbDisks.Number -and $null -ne $_.AccessPaths } | Select-Object -ExpandProperty AccessPaths if (-not $UsbAccessPaths) { return } # Match volumes by GUID path, which is present in both Get-Volume and Get-Partition access paths $Volumes = Get-Volume | Sort-Object DriveLetter | Where-Object { $_.Path -in $UsbAccessPaths } | Select-Object DriveLetter, FileSystemLabel, FileSystem, @{Name = 'SizeGB'; Expression = { [int]($_.Size / 1GB) }}, @{Name = 'SizeRemainingGB'; Expression = { [int]($_.SizeRemaining / 1GB) }}, @{Name = 'SizeRemainingMB'; Expression = { [int]($_.SizeRemaining / 1MB) }}, DriveType, OperationalStatus, HealthStatus, UniqueId if ($PSBoundParameters.ContainsKey('FileSystemLabel')) { return ($Volumes | Where-Object { $_.FileSystemLabel -eq $FileSystemLabel }) } $Volumes } |