I want to write something like nestat
(that log new tcp connections to console) but for pnp devices with powershell.
Is there an API method "to subscribe to some kind of events bus" specific to PnP and get "connected" and "disconnected" events?
Or only one way to achive this is looping with Get-PnpDevice
and "manually" seacrh for the differences?
You can use WMI events to do this. For example, here is one way to do it (seems over-complicated, so maybe someone can improve on it):
Now, when you add/remove a device, you'll get output like this in the console:
A couple of things to keep in mind:
As mentioned, the registrations should be cancelled when your session ends, but if you want to do it manually, you can do it like this: