Switch Bluetooth off and on via PowerShell

Copper Contributor

Hi,
is it possible to switch Bluetooth off and on via PowerShell?

I only found how to stop and start the Bluetooth service and drivers but that does not solve our issue.
See picture below

 

Bluetooth_on_off.jpg

Kind regards

   Cornelius

 

4 Replies

@corichter 

Yes

https://superuser.com/questions/1168551/turn-on-off-bluetooth-radio-adapter-from-cmd-powershell-in-w...

[CmdletBinding()] Param (
    [Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$BluetoothStatus
)
If ((Get-Service bthserv).Status -eq 'Stopped') { Start-Service bthserv }
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
    $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
    $netTask = $asTask.Invoke($null, @($WinRtTask))
    $netTask.Wait(-1) | Out-Null
    $netTask.Result
}
[Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
[Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
$radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
$bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' }
[Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null

 

Hi,
thanks for the response. It does not work, I get a "Deniedbyuser" as stated also in this article:

https://superuser.com/questions/1615053/using-powershell-to-turn-on-off-the-bluetooth-script-doesnt-...

Anyone found a solution about this?
Kind regards
Cornelius
Hi,

did some more investigations on this issue.
The problem seems to be related to the Radio.RequestAccessAsync() object.
Found this C# example https://stackoverflow.com/questions/34602074/radio-requestaccessasync-only-return-deniedbyuser
How to do this in PowerShell?
Kind regards
Hi All,
The solution to fix "DeniedByUser" error in this case is to add a script to change the value for registry key "LetAppsAccessRadios" to "0" in registry path HKLM:\SOFTWARE\Policies\Microsoft\Windows\AppPrivacy

Define a variable:
$RegPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\AppPrivacy"
$RegKey = "LetAppsAccessRadios"
In script:
Set-ItemProperty -Path $RegPath -Name $RegKey -type DWORD -Value 0
Hope this helps. Thanks.

Regards,
Vivyn C R S