Switch Bluetooth off and on via PowerShell

%3CLINGO-SUB%20id%3D%22lingo-sub-2275616%22%20slang%3D%22en-US%22%3ESwitch%20Bluetooth%20off%20and%20on%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2275616%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3CBR%20%2F%3Eis%20it%20possible%20to%20switch%20Bluetooth%20off%20and%20on%20via%20PowerShell%3F%3C%2FP%3E%3CP%3EI%20only%20found%20how%20to%20stop%20and%20start%20the%20Bluetooth%20service%20and%20drivers%20but%20that%20does%20not%20solve%20our%20issue.%3CBR%20%2F%3ESee%20picture%20below%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Bluetooth_on_off.jpg%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F273275iDC455B9B29B2DC5B%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Bluetooth_on_off.jpg%22%20alt%3D%22Bluetooth_on_off.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EKind%20regards%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3BCornelius%3C%2FP%3E%3CP%3E%E2%80%83%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2275616%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2277319%22%20slang%3D%22en-US%22%3ERe%3A%20Switch%20Bluetooth%20off%20and%20on%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2277319%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1025533%22%20target%3D%22_blank%22%3E%40corichter%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fsuperuser.com%2Fquestions%2F1168551%2Fturn-on-off-bluetooth-radio-adapter-from-cmd-powershell-in-windows-10%2F1293303%231293303%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fsuperuser.com%2Fquestions%2F1168551%2Fturn-on-off-bluetooth-radio-adapter-from-cmd-powershell-in-windows-10%2F1293303%231293303%3C%2FA%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%5BCmdletBinding()%5D%20Param%20(%0A%20%20%20%20%5BParameter(Mandatory%3D%24true)%5D%5BValidateSet('Off'%2C%20'On')%5D%5Bstring%5D%24BluetoothStatus%0A)%0AIf%20((Get-Service%20bthserv).Status%20-eq%20'Stopped')%20%7B%20Start-Service%20bthserv%20%7D%0AAdd-Type%20-AssemblyName%20System.Runtime.WindowsRuntime%0A%24asTaskGeneric%20%3D%20(%5BSystem.WindowsRuntimeSystemExtensions%5D.GetMethods()%20%7C%20%3F%20%7B%20%24_.Name%20-eq%20'AsTask'%20-and%20%24_.GetParameters().Count%20-eq%201%20-and%20%24_.GetParameters()%5B0%5D.ParameterType.Name%20-eq%20'IAsyncOperation%601'%20%7D)%5B0%5D%0AFunction%20Await(%24WinRtTask%2C%20%24ResultType)%20%7B%0A%20%20%20%20%24asTask%20%3D%20%24asTaskGeneric.MakeGenericMethod(%24ResultType)%0A%20%20%20%20%24netTask%20%3D%20%24asTask.Invoke(%24null%2C%20%40(%24WinRtTask))%0A%20%20%20%20%24netTask.Wait(-1)%20%7C%20Out-Null%0A%20%20%20%20%24netTask.Result%0A%7D%0A%5BWindows.Devices.Radios.Radio%2CWindows.System.Devices%2CContentType%3DWindowsRuntime%5D%20%7C%20Out-Null%0A%5BWindows.Devices.Radios.RadioAccessStatus%2CWindows.System.Devices%2CContentType%3DWindowsRuntime%5D%20%7C%20Out-Null%0AAwait%20(%5BWindows.Devices.Radios.Radio%5D%3A%3ARequestAccessAsync())%20(%5BWindows.Devices.Radios.RadioAccessStatus%5D)%20%7C%20Out-Null%0A%24radios%20%3D%20Await%20(%5BWindows.Devices.Radios.Radio%5D%3A%3AGetRadiosAsync())%20(%5BSystem.Collections.Generic.IReadOnlyList%5BWindows.Devices.Radios.Radio%5D%5D)%0A%24bluetooth%20%3D%20%24radios%20%7C%20%3F%20%7B%20%24_.Kind%20-eq%20'Bluetooth'%20%7D%0A%5BWindows.Devices.Radios.RadioState%2CWindows.System.Devices%2CContentType%3DWindowsRuntime%5D%20%7C%20Out-Null%0AAwait%20(%24bluetooth.SetStateAsync(%24BluetoothStatus))%20(%5BWindows.Devices.Radios.RadioAccessStatus%5D)%20%7C%20Out-Null%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2296357%22%20slang%3D%22en-US%22%3ERe%3A%20Switch%20Bluetooth%20off%20and%20on%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2296357%22%20slang%3D%22en-US%22%3EHi%2C%3CBR%20%2F%3Ethanks%20for%20the%20response.%20It%20does%20not%20work%2C%20I%20get%20a%20%22Deniedbyuser%22%20as%20stated%20also%20in%20this%20article%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fsuperuser.com%2Fquestions%2F1615053%2Fusing-powershell-to-turn-on-off-the-bluetooth-script-doesnt-work%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsuperuser.com%2Fquestions%2F1615053%2Fusing-powershell-to-turn-on-off-the-bluetooth-script-doesnt-work%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EAnyone%20found%20a%20solution%20about%20this%3F%3CBR%20%2F%3EKind%20regards%3CBR%20%2F%3ECornelius%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2363123%22%20slang%3D%22en-US%22%3ERe%3A%20Switch%20Bluetooth%20off%20and%20on%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2363123%22%20slang%3D%22en-US%22%3EHi%2C%3CBR%20%2F%3E%3CBR%20%2F%3Edid%20some%20more%20investigations%20on%20this%20issue.%3CBR%20%2F%3EThe%20problem%20seems%20to%20be%20related%20to%20the%20Radio.RequestAccessAsync()%20object.%3CBR%20%2F%3EFound%20this%20C%23%20example%20%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F34602074%2Fradio-requestaccessasync-only-return-deniedbyuser%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F34602074%2Fradio-requestaccessasync-only-return-deniedbyuser%3C%2FA%3E%3CBR%20%2F%3EHow%20to%20do%20this%20in%20PowerShell%3F%3CBR%20%2F%3EKind%20regards%3C%2FLINGO-BODY%3E
New 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