# Script to turn Bluetooth on/off on Windows devices

Toggling a device’s Bluetooth Settings will require the user to modify it through the system settings. As a system admin, if you need to change the Bluetooth state of multiple devices, it can prove to be an arduous task. In such situations, the [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) action is here to help.

 Disclaimer: 
The sample script provided below is adapted from third-party open-source sites.

 

 Toggle Bluetooth status 
-------------------------

You can use the following PowerShell script to turn Bluetooth on/off.

Script to toggle Bluetooth status

\[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

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

  \[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

   

 

  

Pass the arguments ‘**ON**’ or ‘**OFF**’ while running this script to enable or disable Bluetooth on devices.

 Notes:- The argument is not case-sensitive.
- A delay of a few minutes can be expected while executing the script to turn Bluetooth on/off since it involves restarting the Bluetooth daemon.
- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.