{"id":4540,"date":"2012-11-30T00:01:00","date_gmt":"2012-11-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/11\/30\/use-powershell-and-wmi-to-find-wireless-keyboard-mouse\/"},"modified":"2012-11-30T00:01:00","modified_gmt":"2012-11-30T00:01:00","slug":"use-powershell-and-wmi-to-find-wireless-keyboard-mouse","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-and-wmi-to-find-wireless-keyboard-mouse\/","title":{"rendered":"Use PowerShell and WMI to Find Wireless Keyboard &#038; Mouse"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and WMI to find a wireless keyboard or mouse attached to a system.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, we had a great time at the Windows PowerShell user group meeting at the Microsoft Office in Munich (actually just outside of Munich). It was great to be back&mdash;as it had been five years since I was in the building. The Scripting Wife and I got to see a lot of our old friends.<\/p>\n<p>Today, we are supposed to meet with Windows PowerShell Guru Oliver Lipkau. We&#8217;ll see him in either Munich or in Regensburg&mdash;not sure which town yet. We are in our hotel room right now waiting for his email to detail the plans, and I am sipping an excellent cup of Earl Grey tea&mdash;Germany has awesome tea because so much comes through the port of Hamburg. It does not matter where we meet him because both towns are absolutely lovely.<\/p>\n<h2>Use WMI to detect wireless keyboard and mouse<\/h2>\n<p>One of the things I remember from when I was a consultant is that my customers wanted to be able to know who was using a wireless keyboard or a wireless mouse. This was for inventory purposes, audit purposes, or maybe general information. Anyway, at the time, it was not possible to obtain this information because the two WMI classes (<strong>Win32_Keyboard<\/strong> and <strong>Win32_PointingDevice<\/strong>) could not detect a wireless device back then. It only reported the &ldquo;primary devices&rdquo; physically connected to the computers. Bummer.<\/p>\n<p>At some point, WMI started reporting wireless devices. I do not have the resources to test this out right now, so I cannot report when exactly WMI began reporting wireless keyboards and mice. However, I will use both Windows PowerShell&nbsp;3.0 and Windows PowerShell&nbsp;2.0 code so you can test this out for yourself. Remember we are in the middle of a three-week trip, and I did not even pack a &ldquo;winter coat&rdquo; let alone three different laptops running various versions of Windows. I do know it works on my Windows&nbsp;8 laptop.<\/p>\n<h2>First, the wireless mouse<\/h2>\n<p>Ok, it is a mouse, right? Well, WMI calls it a &#8220;point device,&#8221; and therefore, the WMI class name is <strong>Win32_PointingDevice<\/strong>. I can get an inventory of the attached mice by querying the class. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_PointingDevice<\/p>\n<p>With Windows PowerShell&nbsp;3.0, use the <strong>Get-CimInstance<\/strong> cmdlet, as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance Win32_PointingDevice<\/p>\n<p>The output displays a lot of stuff&mdash;some of which is not populated. Here is a screen shot of the query and some of the output.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2625.hsg-11-30-12-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2625.hsg-11-30-12-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>A better output appears when I pipe the results to the <strong>HasWMIValue<\/strong> filter from my Scripting Guys WMI module. This filter displays only WMI properties from a class that has values. It makes a much cleaner output. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6404.hsg-11-30-12-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6404.hsg-11-30-12-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Ok, I found what I was looking for&mdash;a caption that says <strong>HID-compliant mouse<\/strong>. I could have found this piece of information in Devices and Printers. In fact, I can even launch it through the <strong>Show-ControlPanelItem<\/strong> cmdlet as seen here.<\/p>\n<p style=\"padding-left: 30px\">Show-ControlPanelItem devices*<\/p>\n<p>Here is an image of the property page for the mouse.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2248.hsg-11-30-12-03.png\"><img decoding=\"async\" title=\"Image of property page for mouse\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2248.hsg-11-30-12-03.png\" alt=\"Image of property page for mouse\" \/><\/a><\/p>\n<p>From a management perspective, the two most important properties are the description and the PNPDeviceID. I use the <strong>Get-WmiObject<\/strong> cmdlet to return both mice; then I use the <strong>Where-Object<\/strong> to filter out only the HID type; and, lastly, I send the information to a <strong>Format-Table<\/strong> command to produce a nice output, as shown here.<\/p>\n<p style=\"padding-left: 30px\"><strong>Windows PowerShell 2.0 syntax:<\/strong><\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_PointingDevice | Where-Object { $_.Description -match &#8216;hid&#8217;} | Format-Table description, pnpDeviceID -AutoSize -Wrap<\/p>\n<p style=\"padding-left: 30px\"><strong>Windows PowerShell 3.0 syntax:<\/strong><\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance win32_PointingDevice | Where Description -match &#8216;hid&#8217; | Format-Table description, pnpDeviceID -AutoSize &ndash;Wrap<\/p>\n<p>Here is the result of both commands.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4530.hsg-11-30-12-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4530.hsg-11-30-12-04.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Next, the wireless keyboard<\/h2>\n<p>Just like I can use WMI to find the HID wireless mouse, I can also use it to find the HID wireless keyboard. I use the WMI class Win32_Keyboard. Here is the result of piping the class to the <strong>HasWMIValue <\/strong>filter&mdash;this is everything WMI will tell me.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4530.hsg-11-30-12-05.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4530.hsg-11-30-12-05.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Once again, the two properties I am interested in are the PNPDeviceID and the description. Here are the commands used to produce my &ldquo;report.&rdquo;<\/p>\n<p><strong>Windows PowerShell 2.0 syntax:<\/strong><\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_Keyboard | Where-Object { $_.Description -match &#8216;hid&#8217;} | Format-Table description, pnpDeviceID -AutoSize -Wrap<\/p>\n<p><strong>Windows PowerShell 3.0 syntax:<\/strong><\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance win32_keyboard | Where Description -match &#8216;hid&#8217; | Format-Table description, pnpDeviceID -AutoSize &ndash;Wrap<\/p>\n<p>The results of running these two commands is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6786.hsg-11-30-12-06.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6786.hsg-11-30-12-06.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Dude! They are basically the same&mdash;combine &lsquo;em<\/h2>\n<p>One of the nice things about WMI is that at times it is very consistent. As it turns out, this is one of those occasions. I am filtering by &lsquo;hid&rsquo; and selecting the same two properties: description and pnpDeviceID. This way I can create an array of WMI classes and do both queries at the same time, as shown here, where <strong>gwmi<\/strong> is an alias for <strong>Get-WmiObject<\/strong>, <strong>%<\/strong> is an alias for <strong>foreach-object<\/strong>, <strong>?<\/strong> is an alias for the <strong>Where-Object<\/strong>, and <strong>ft<\/strong> is an alias for <strong>Format-Table<\/strong>.<\/p>\n<p style=\"padding-left: 30px\">$class = &#8220;win32_pointingdevice&#8221;,&#8221;win32_keyboard&#8221;<\/p>\n<p style=\"padding-left: 30px\">$class | % {gwmi $_ | ? description -match &#8216;hid&#8217;} | ft description, PNPDeviceID -A -Wr<\/p>\n<p>Here is the output from the command.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8726.hsg-11-30-12-07.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8726.hsg-11-30-12-07.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Well, that is enough for this morning. We need to go hop a train. Join me tomorrow for some more Windows PowerShell fun.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and WMI to find a wireless keyboard or mouse attached to a system. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, we had a great time at the Windows PowerShell user group meeting at the Microsoft Office in Munich (actually just outside of Munich). [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[385,34,35,3,4,45,6],"class_list":["post-4540","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-cim","tag-hardware","tag-peripherals-and-devices","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and WMI to find a wireless keyboard or mouse attached to a system. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, we had a great time at the Windows PowerShell user group meeting at the Microsoft Office in Munich (actually just outside of Munich). [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4540"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4540\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}