How to list bluetooth devices near me using PowerShell - windows

How to list all Bluetooth devices paired or currently near me and particullary MAC adress ? I want the equivalent of the command :
netsh wlan show network mode=bssid
I am able to list all Bluetooth devices and characteristics but not MAC adress with the command :
Get-PnpDevice | Where-Object {$_.Class -eq "Bluetooth"}
foreach ($device in $devices) { echo $device.InstanceId }
Note it is not a problem if I need to manually shrink results and if the list is not in a perfect layout.

The PowerShell command :
Get-ChildItem -Path HKLM:\SYSTEM\ControlSet001\Services\DeviceAssociationService\State\Store\ | Select-String -Pattern Bluetooth
will print devices already paired :
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DeviceAssociationService\State\Store\Bluetooth#BluetoothXX:XX:XX:XX:XX:b2-YY:YY:YY:YY:YY:YY
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DeviceAssociationService\State\Store\Bluetooth#BluetoothXX:XX:XX:XX:XX:b2-ZZ:ZZ:ZZ:ZZ:ZZ:ZZ
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DeviceAssociationService\State\Store\BluetoothLE#BluetoothLEXX:XX:XX:XX:XX:b2-WW:WW:WW:WW:WW:WW
The XX:XX:XX:XX:XX values is your Bluetooth MAC adress.

Related

How to get Bluetooth device battery percentage using PowerShell on windows?

I am trying to plot a graph for Bluetooth headphone battery discharge. For that I need to read battery percentage of the connected device. I can see power information is available on GUI for the device. Is there any way to get the battery percentage info for connected Bluetooth device using PowerShell? (like using wmi or anything else)
In my findings, you can get information on Bluetooth devices using the Get-PnpDevice cmdlet. This should return a list of PnP Devices, their Status, Class, FriendlyName and InstanceID.
Get-PnpDevice
You can filter the results with -Class parameter. To specify Bluetooth PnP devices, you can enter "Bluetooth" as a string value for the -Class parameter.
Get-PnpDevice -Class 'Bluetooth'
You can then specify the device you have in mind from this list by their FriendlyName using the -FriendlyName parameter and enter the desired device's FriendlyName as a string value for the parameter.
Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName'
Note: You can also specify the device using the -InstanceId parameter and providing the device's InstanceId as a string value for the parameter.
If you then pipe the previous command to the Get-PnpDeviceProperty cmdlet, it will return a list of the device's properties, including its InstanceId, KeyName, Type and Data.
Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
Get-PnpDeviceProperty
Beyond this point, I was able to further filter the results of the command by using the -KeyName parameter and entering the KeyName of the property that (I assume) contains Device Power Data as a string value for the parameter.
Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
Get-PnpDeviceProperty -KeyName 'PropertyKeyName'
Unfortunately this is as far as I've gotten to solving the problem. Hopefully my contribution helps.
As far as I am aware, there is no way to poll bluetooth device data beyond what you would get with Get-WmiObject, since the battery status seen in Windows Settings -> Bluetooth Devices is something coming from the vendors/devices driver and seems to, as of now, be inaccessible by PowerShell, unless there is some exotic snapin I am not aware of.
You can get all possible device information via this command:
Get-WmiObject -Query "select * from win32_PnPEntity" | Where Name -like "MyDeviceName"
Or if you are unsure how the device is named as of now, this would return a complete list of "devices":
Get-WmiObject -Query "select * from win32_PnPEntity" | Select Name
Additionally, I couldn't find battery information in the registry - maybe someone more knowledge can expand on that because the registry probably contains the necessary information as it must be stored somewhere on the device.
Paulo Amaral's comment above is indeed the answer.
The code below will provide you with a command you can reuse to query the battery state of your device:
Get-PnpDevice -FriendlyName "*<Device Name>*" | ForEach-Object {
$local:test = $_ |
Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' |
Where Type -ne Empty;
if ($test) {
"To query battery for $($_.FriendlyName), run the following:"
" Get-PnpDeviceProperty -InstanceId $($test.InstanceId) -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data"
""
"The result will look like this:";
Get-PnpDeviceProperty -InstanceId $($test.InstanceId) -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data
}
}
For my <Headset> device, the output looked similar to:
To query battery for <Headset>, run the following:
Get-PnpDeviceProperty -InstanceId BTHENUM\{0000####-0000-####-####-############}_VID&########_PID&####\#&########&#&############_######### -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2'
The result will look like this:
90

How to find the wifi adapter for different languages with PowerShell

A powershell script should enable/disable the wireless network adapter.
So i make use of the NetAdaper cmdlet.
Get-NetAdapter
Enable-NetAdapter -Name 'Wi-Fi'-Confirm:$false
Disable-NetAdapter -Name 'Wi-Fi'-Confirm:$false
Its works fine on english language systems.
On system with other languages configured it fails due to the wireless adapter does not have "Wi-Fi" as name.
Example would be german.
Enable-NetAdapter -Name 'WLAN'-Confirm:$false
Disable-NetAdapter -Name 'WLAN'-Confirm:$false
How to archive this script running on all languages?
Get-NetAdapter has a field named InterfaceType which contains a value based on this enum:
https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterfacetype?view=net-5.0
According to that documentation, type 71 is a Wireless80211 device.
Just to show it's there:
[int][System.Net.NetworkInformation.NetworkInterfaceType]::Wireless80211
So you could do something like this:
Get-NetAdapter | Where-Object InterfaceType -eq 71
Which will return a list of all wireless adapters.

Disable Exclusive Mode for all audio devices in Windows 10

I need to configure audio devices a large number of PCs.
I am stuck on how to Disable Exclusive Mode, "allow applications to take exclusive-mode of this device"
I can disable all the media devices:
Get-PnpDevice -class "MEDIA" | ForEach-Object {Disable-PnpDevice -InstanceId $_.InstanceID -Confirm:$false}
I can enable the desired devices:
Get-PnpDevice -Class Media | where FriendlyName -like "*NVIDIA High Definition Audio*" | where Status -eq "OK" | ForEach-Object { Disable-PnpDevice -instanceid $_.instanceid -Confirm:$false }
I can set the default audio device (playback/recording) leveraging AudioDeviceCmdlets.
I can even set the sound reduction to 0 through the registry
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Multimedia\Audio] "UserDuckingPreference"=dword:00000003
But I cannot find an API or a data structure that corresponds to where the state is changed and how?
Can someone point me in the right direction here?
These parameters are available in the registry key:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{xxx-xxx-xxx-xxx}\Properties
(change XXX by the GUID of your default record device)
The names of the keys are:
{b3f8fa53-0004-438e-9003-51a46e139bfc},3
{b3f8fa53-0004-438e-9003-51a46e139bfc},4
The value can be 0 or 1.
This correspond to the check box "Allow applications to take exclusive control of this device" and "Give exclusive mode applications priority"
But those key are owned by system account and cant be edited easily with a script.
If you are OK to use external program, a nirsoft sofware utility can do the job:
Nirsoft SoundVolumeView
You can use it from cli:
SoundVolumeView.exe /SetExclusivePriority "XXXXX" 0
SoundVolumeView.exe /SetAllowExclusive "XXXXXX" 0
(XXX can be ID or name of the record device)

Win32_PnPEntity not getting hidden devices

I'm using the Win32_PnPEntity class to get all the devices in a computer, but the Win32_PnPEntity class does not list the hidden devices. Hidden devices in the Windows device manager have a status "Currently, this hardware device is not connected to the computer. (Code 45)" and can be shown by clicking the menu option in Device Manager: View > Show hidden devices (Windows 10).
Does anyone know how to get the hidden devices?
You can do it using the command:
Get-PnpDevice -class "Ports"
Status Class FriendlyName
------ ----- ------------
OK Ports Communications Port (COM1)
Unknown Ports Silicon Labs Dual CP2105 USB to UART Bridge: Enhanced
Unknown Ports Arduino Uno (COM5)
Unknown Ports Silicon Labs Dual CP2105 USB to UART Bridge: Standard
OK Ports Prolific USB-to-Serial Comm Port (COM6)
Here you can see my COM ports that have been disconnected (status: unknown)
You can make use of ConfigManagerErrorCode. Refer Win32_PnPEntity and Win32_PnPEntity MSDN. You have not mentioned if you are using powershell or C# for scripting, i am assuming powershell.
$result = #{Expression = {$_.Name}; Label = "Device Name"},
#{Expression = {$_.ConfigManagerErrorCode} ; Label = "Status Code" }
Get-WmiObject -Class Win32_PnpEntity -ComputerName localhost -Namespace Root\CIMV2 | Where-Object {$_.ConfigManagerErrorCode -gt 0 } | Format-Table $result

How to find the drive letter of a newly inserted USB drive using Powershell?

I am trying to accomplish this:
When I insert a USB drive in Windows (Win7 and Win8), I want a script to be started (Powershell or batch command) automatically. The machine has autoplay disabled.
I setup a scheduled task with event id 2010 in DriveFrameworks-UserMode as trigger. The scheduled task works. However, I am having difficulties finding the drive letter of the newly inserted USB drive.
I found a few solutions on the Internet, but they didn't fit my requirements. The solutions either check for all drive letters (A to Z), or check for USB drive. I would like to correlate the drive letter that is associated with the event in the DriveFrameworks-UserMode trigger.
This post (How do i get the drive letter of a USB Drive in Powershell?) gave me some hints and I checked classes win32_logicaldisk, win32_diskdrive, and win32_pnpentity, but I couldn't find a clue matching them.
Any help is appreciated.
This should help:
$diskdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"}
$letters = $diskdrive | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_. deviceid}
$drive = gwmi win32_volume | ? {$letters -contains ($_.name -replace "\\")}
$drive.DriveLetter
PS. There is a similar discussion here

Resources