Wednesday, June 19, 2024

How to discover Bluetooth connections with PowerShell?

 

About PowerShell:


Powershell is one of Microsoft's extremely useful and powerful tool. It is installed on most of the Microsoft computers and laptops.

Just open type in PowerShell in Windows Search as shown.

The Search brings up the following screen:

There are two versions of PowerShell for x86 and x64 architectures and there is also an Integrated Scripting Environment(ISE), a very useful option that can do a lot more than the command line tool.

I will be using this to discover Bluetooth devices on my laptop that are discoverable.

Let me open the x64 bit version with Administrator rights (also known as elevated permissions). I do this because, the program may not allow you to do certain things otherwise.


The red arrows are Running the code, or running a section of the code that you choose to run. Since, we are not using the ISE, these are greyed out.

Working with Bluetooth:

Bluetooth is a Pnp device and it is called Bluetooth. Run the following code as shown:


PS C:\WINDOWS\system32> Get-PnpDevice | Where-Object {$_.Class -eq "Bluetooth"}

After typing the code click Enter key. The following is displayed,

=====================

PS C:\WINDOWS\system32> Get-PnpDevice | Where-Object {$_.Class -eq "Bluetooth"}


Status     Class           FriendlyName                                                                     InstanceId     

------     -----           ------------                                                                     ----------     

OK         Bluetooth       Object Push Service                                                              BTHENUM\{000...

OK         Bluetooth       Bluetooth LE Generic Attribute Service                                           BTHLEDEVICE\...

OK         Bluetooth       Generic Attribute Profile                                                        BTHLEDEVICE\...

Unknown    Bluetooth       Jayaram's S23+ Avrcp Transport                                                   BTHENUM\{000...

OK         Bluetooth       Microsoft Bluetooth Enumerator                                                   BTH\MS_BTHBR...

OK         Bluetooth       Generic Access Profile                                                           BTHLEDEVICE\...

OK         Bluetooth       Intel(R) Wireless Bluetooth(R)                                                   USB\VID_8087...

OK         Bluetooth       Jayaram's S23+ Avrcp Transport                                                   BTHENUM\{000...

OK         Bluetooth       Microsoft Bluetooth LE Enumerator                                                BTH\MS_BTHLE...

OK         Bluetooth       Phonebook Access Pse Service                                                     BTHENUM\{000...

OK         Bluetooth       Personal Area Network Service                                                    BTHENUM\{000...

OK         Bluetooth       Bluetooth Device (RFCOMM Protocol TDI)                                           BTH\MS_RFCOM...

OK         Bluetooth       Jayaram's S23+                                                                   BTHLE\DEV_6A...

OK         Bluetooth       Jayaram's S23+                                                                   BTHENUM\DEV_...

OK         Bluetooth       Bluetooth LE Generic Attribute Service                                           BTHLEDEVICE\...

Unknown    Bluetooth       Jayaram's S23+ Avrcp Transport                                                   BTHENUM\{000...

OK         Bluetooth       Personal Area Network NAP Service                                                BTHENUM\{000...

OK         Bluetooth       Bluetooth LE Generic Attribute Service                                           BTHLEDEVICE\...

OK         Bluetooth       Jayaram's S23+ Avrcp Transport                                                   BTHENUM\{000...

===========================================

The InstanceID cannot be fully seen.

You need to format the output. The best way is to run the following:

PS C:\WINDOWS\system32>  Get-PnpDevice | Where-Object {$_.Class -eq "Bluetooth"} | Export-Csv -Path "C:\Users\Me\Desktop\listing.csv" -NoTypeInformation

This will provide a more complete output in the exported listing.csv file at the indicated location.

The CSV file has much more information in tabulated columns.


Although this gives some idea of connected devices, there are repetitions and somewhat difficult to look for specific information.

I not only want Bluetooth devices discovered but I would also like to get the ones that are ready for pairing with the laptop's Bluetooth.

This next code and the response makes it little more clear.

================================

PS C:\WINDOWS\system32> $devices = Get-ChildItem -Path HKLM:\\SYSTEM\\ControlSet001\\Services\\BTHPORT\\Parameters\\Devices

foreach ($device in $devices) {

    $address = $device.pschildname.ToUpper()

    $name = $device.GetValue("Name")

    if ($name -ne $null) {

        $printableName = ($name -notmatch 0 | ForEach-Object {[char]$_}) -join ""

        echo "Address: $address, Name: $printableName"

    }

}

Here is the response:

Address: 6A185BABF058, Name: Jayara's S3+

Address: 9C73B16CBD32, Name: Jayara's S3+

Address: FB621134FEF9, Name: KS01


PS C:\WINDOWS\system32> 

========================

In the above, I see my paired Samsung S23+ Smartphone and a Smartwatch, KS01. However, I do not see the paired Samsung Smart TV. That's something I need to troubleshoot.

In this post, discovering devices connected by Bluetooth was correctly identified using the PowerShell.


More on PowerShell here: Just search for PowerShell in this blog and You can get dozens of posts that deal with all aspects of PowerSehell.



No comments: