8

I have a very slow and relatively cheap computer. When I turn it on, I turn on my Bluetooth mouse. The mouse works fine for a few seconds, then the connection is broken. If I then reconnect the mouse, it works as it should be until I turn it off again.

My goal is: I would like to write a PowerShell script that will reconnect the mouse automatically, but I don't know how it works with Bluetooth in PowerShell. Could anyone help me with that?

deralbert
  • 856
  • 2
  • 15
  • 33
AllAwesome497
  • 190
  • 2
  • 3
  • 14

2 Answers2

15

Try something like this, note that elevated permissions are required:

$device = Get-PnpDevice -class Bluetooth -friendlyname "FriendlyDeviceName"
Disable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false
Start-Sleep -Seconds 10
Enable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false

This script disables the device and after 10 seconds enables it again.

Simon
  • 23
  • 5
Bernard Moeskops
  • 1,203
  • 1
  • 12
  • 16
  • Ok i am working on setting that up, but I won’t be able to finish and try it until this evening thanks. – AllAwesome497 Dec 07 '18 at 13:49
  • 1
    Ok whenever I run that (as an admin) it gives me this error: _Disable-PnpDevice : Not supported At line:2 char:1 + Disable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotImplemented: (Win32_PnPEntity...B17A1&0&FAD...):ROOT\cimv2\Win32_PnPEntity) [Disable-PnpDevice], CimException + FullyQualifiedErrorId : HRESULT 0x8004100c,Disable-PnpDevice_ – AllAwesome497 Dec 09 '18 at 18:05
  • 1
    This worked great for me! I have a Google Home device I'm using as a speaker for a PC and it often is disconnected for some reason, so I set up this script as a quick way to resolve. – Kevin Gwynn Jan 24 '21 at 20:56
  • Doesn't seem to work on Win11. No response or error after I run the command and nothing connects – swerly Jan 09 '23 at 19:26
  • I have just tested it in Windows 11 and it seems to be working. If you would just do 'Get-PnpDevice -class Bluetooth', it should list all devices. Then just choose the correct friendlyName to use in the script. – Bernard Moeskops Jan 10 '23 at 09:00
3

Try this Powershell script, it disables, and unpairs the Bluetooth peripheral (source). Many thanks to xzion14!

$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 |
      ?{$_.HardwareID -match 'DEV_'} |
         select Status, Class, FriendlyName, HardwareID,
            # Extract device address from HardwareID
            @{N='Address';E={[uInt64]('0x{0}' -f $_.HardwareID[0].Substring(12))}}
}
################## Execution Begins Here ################
$BTDevices = @(Get-BTDevice) # Force array if null or single item
$BTR = Add-Type -MemberDefinition $Source -Name "BTRemover"  -Namespace "BStuff" -PassThru
Do {
   If ($BTDevices.Count) {
      "`n******** Bluetooth Devices ********`n" | Write-Host
      For ($i=0; $i -lt $BTDevices.Count; $i++) {
         ('{0,5} - {1}' -f ($i+1), $BTDevices[$i].FriendlyName) | 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)
David Buck
  • 3,752
  • 35
  • 31
  • 35