
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check USB Device Connection Using PowerShell
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'}
Advertisements