How to check if the USB device is connected to the system using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To retrieve the USB-connected devices using Powershell, we need to retrieve all the drives using the WMI object or CIM Instance and need to filter the win32_diskdrive class with the USB as shown below.

So basically, USB devices and other removable devices have the drivetype '2'. You can search with the InterfaceType or the DriveType.

WMI commands,

gwmi win32_diskdrive | where{$_.Interfacetype -eq "USB"}

Alternatively,

With the CIM commands,

Get-CimInstance -ClassName Win32_DiskDrive | where{$_.InterfaceType -eq 'USB'}

or

Get-CimInstance -ClassName Win32_LogicalDisk | where{$_.DriveType -eq '2'}

If there is no USB device connected to the system, there will be no output. To retrieve the USB disk on the remote computer use −ComputerName parameter.

Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName TestMachine | where{$_.DriveType -eq '2'}
raja
Published on 17-May-2021 12:00:42
Advertisements