You cannot just use the cmdlets mentioned by yourself and @Ash to accomplish your use case...
'...detect when a USB printer is plugged in...'
You have to have an active event listening for that.
Example: for a USB disk.
Register-WmiEvent -Query "Select * from __InstanceCreationEvent within 5 where targetinstance isa 'win32_logicaldisk'" -SourceIdentifier disk -Timeout 1000
Get-Event -SourceIdentifier disk
$DiskInsertEvent = Get-Event -SourceIdentifier disk
$DiskInsertEvent.SourceEventArgs.NewEvent.TargetInstance
Or messaging the user on the event...
Register-WmiEvent -Query "select * from __InstanceCreationEvent within 5 where TargetInstance ISA 'Win32_PnpEntity' and TargetInstance.Description like '%USB Mass Storage Device%'" -Action { Write-Host "Please talk to IT about your USB device." -ForegroundColor Red }
Of course, you need to specify the correct device type.
Or any USB device
Register-WmiEvent -Class win32_DeviceChangeEvent -SourceIdentifier deviceChange
$newEvent = Wait-Event -SourceIdentifier deviceChange
Then immediately query what was inserted via the cmdlets already mentioned by you and @Ash
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.