Skip to content

Instantly share code, notes, and snippets.

@ironiridis
Last active January 30, 2020 05:23
Show Gist options
  • Save ironiridis/ec8f8777ac00849c8b5def7b559b4e72 to your computer and use it in GitHub Desktop.
Save ironiridis/ec8f8777ac00849c8b5def7b559b4e72 to your computer and use it in GitHub Desktop.
Bluetooth Unpair Utility
# Originally from this forum post:
# https://www.tenforums.com/drivers-hardware/22049-how-completely-remove-bluetooth-device-win-10-a-post1630800.html#post1630800
# Update to get hardware ID via a string split
# Add hardware IDs to user-visible output
# Don't display IDs that are zero, since they can't be removed via this API anyway
$Source = @"
[DllImport("BluetoothAPIs.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.U4)]
static extern UInt32 BluetoothRemoveDevice(IntPtr pAddress);
public static UInt32 Unpair(UInt64 BTAddress) {
GCHandle pinnedAddr = GCHandle.Alloc(BTAddress, GCHandleType.Pinned);
IntPtr pAddress = pinnedAddr.AddrOfPinnedObject();
UInt32 result = BluetoothRemoveDevice(pAddress);
pinnedAddr.Free();
return result;
}
"@
Function Get-BTDevice {
Get-PnpDevice -class Bluetooth |
Where-Object{$_.HardwareID -match 'DEV_'} |
Select-Object Status, Class, FriendlyName, HardwareID,
# Extract device address from HardwareID
@{N='Address';E={[uInt64]('0x{0}' -f $_.HardwareID[0].Split("_")[1])}}
}
################## Execution Begins Here ################
$BTR = Add-Type -MemberDefinition $Source -Name "BTRemover" -Namespace "BStuff" -PassThru
$BTDevices = @(Get-BTDevice) # Force array if null or single item
Do {
If ($BTDevices.Count) {
"`n******** Bluetooth Devices ********`n" | Write-Host
For ($i=0; $i -lt $BTDevices.Count; $i++) {
if ($BTDevices[$i].Address -gt 0) {
('{0,5} - {1} - {2:x}' -f ($i+1), $BTDevices[$i].FriendlyName, $BTDevices[$i].Address) | Write-Host
}
}
$selected = Read-Host "`nSelect a device to remove (0 to Exit)"
If ([int]$selected -in 1..$BTDevices.Count) {
'Removing device: {0}' -f $BTDevices[$Selected-1].FriendlyName | Write-Host
$Result = $BTR::Unpair($BTDevices[$Selected-1].Address)
If (!$Result) {"Device removed successfully." | Write-Host}
Else {"Sorry, an error occured." | Write-Host}
}
}
Else {
"`n********* No devices foundd ********" | Write-Host
}
} While (($BTDevices = @(Get-BTDevice)) -and [int]$selected)
@ironiridis
Copy link
Author

Unfortunately, while this appears to be working to remove regular old Bluetooth devices, it apparently does not work to remove BLE devices, which was my goal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment