Advertisement
guyrleech

Show new plug & play devices

Feb 27th, 2023 (edited)
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.00 KB | Software | 0 0
  1. ##  show what plug'n'play devices are newly added by comparing devices present at two instances in time
  2.  
  3. $devicesBefore = Get-CimInstance -ClassName Win32_PnPDevice
  4.  
  5. ## wait for devices to change, eg insert a USB device, docking station, etc
  6.  
  7. Get-CimInstance -ClassName Win32_PnPDevice | Where-Object { $with = $_ ; if( -Not $devicesBefore.Where( { $_.SystemElement.deviceid -ieq $with.SystemElement.deviceid } )) { $true } } | Select -ExpandProperty SystemElement | Select -ExpandProperty DeviceID | ForEach-Object { if( $_ -match '^([^\\]+)\\([^&]+)&([^\\&]+)' ) { [pscustomobject]@{ Type = $matches[1] ; Vendor = $matches[2] ; ProductId = $matches[3] ; Raw = $_ } } else { Write-Warning "Failed to parse $_"  } } | sort Type,Vendor,ProductId -Unique
  8.  
  9. ## For devices where a driver is available, there will be events 400 and 410 from provider Microsoft-Windows-Kernel-Pnp
  10. get-winevent -FilterHashtable @{ ProviderName = 'Microsoft-Windows-Kernel-Pnp' ; Id = 400 , 410 ; StartTime = ([Datetime]::Now.AddDays( -1.5 )) }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement