Turn on/off Bluetooth on Windows using Powershell and CMD


👤 Prashant Raj Bista    🕒 25 Feb 2023    📁 FIX

Introduction

Bluetooth is a popular wireless technology that allows you to connect devices. Devices like smartphones, Keyboard, Mouse, and speakers can be connected using Bluetooth. If connected with Bluetooth they will not need any physical connection. Sometimes you might want to turn off your Bluetooth adapter. You might want to disable the Bluetooth adapter to conserve power. Also, restarting adapters can resolve most connection issues. In this article, we will see you how to turn on and off Bluetooth radio/adapter on Windows using PowerShell. But let's first see more about Powershell and Bluetooth.

Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft. Powershell consists of a command-line shell and associated scripting language. 

The scripting language used in PowerShell is based on the .NET framework. These functions can be implemented in scripts. Overall, PowerShell is an important tool for Windows administrators and developers. It provides a powerful and flexible way to automate and manage Windows-based systems.

Bluetooth

Bluetooth is a wireless technology standard for exchanging data over short distances. Bluetooth uses a short wavelength of the frequency range between 2.4 to 2.485 GHz. To work Bluetooth builds a network known as personal area networks (PANs). Bluetooth is commonly used in a variety of devices that support wireless communication. It is used for connecting accessories and peripherals. Such accessories and peripherals include headphones, speakers, mouse, keyboards, and game controllers.

One of the key benefits of Bluetooth technology is that it allows for seamless and wireless communication. Using Bluetooth eliminates the need for cords and cables. That makes it easier to connect and manage multiple devices.

In order to proceed with the execution of the script, you are required to have a windows computer with bluetooth functionality.

Control Bluetooth adapter using Powershell

To control the Bluetooth adapter from Powershell first, you need to enable custom script execution in Powershell. Powershell scripts have extension .ps1. 

To enable the execution of unsigned scripts follow the following steps

Step 1: First, open PowerShell as administrator. Search for PowerShell in the Start menu and right-click on it. You will see an option called Run as administrator.

Open Powershell as administrator
fig. Open Powershell as administrator

Step 2: After that, type the following command in PowerShell.

 Set-ExecutionPolicy Unrestricted 

This above script allows the scripts to be run by all users.

Press y to confirm the execution of the above command.

Set-ExecutionPolicy Unrestricted -Scope CurrentUser

This command allows scripts to be run by the current user without any restrictions.

sets the execution policy for the current user to Unrestricted
fig. sets the execution policy for the current user to Unrestricted

Now let us see how we can enable and disable the Bluetooth adapter using PowerShell.

Step3: First, copy the following PowerShell script to a file. Then save the file with the extension .ps1, which is the extension for PowerShell.

[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
Bluetooth enable disable script
fig. Bluetooth enable disable script
What this script is doing ?

A Windows device's Bluetooth radio can be managed with the help of this application written in PowerShell. To begin, a parameter called $BluetoothStatus is defined. This parameter can take either the value 'On' or the value 'Off.'

The script then checks to see if the Bluetooth service is operating, and if it isn't, it begins the process of starting it. After that, the application makes use of the System. Runtime. In order to communicate with Windows, the WindowsRuntime assembly is required. It does this by defining a function known as Await, which gives the script the ability to wait for asynchronous tasks that are returned by the API.

The script first uses the RequestAccessAsync method to ask for permission to access the Bluetooth radio, and then it uses the GetRadiosAsync method to obtain a list of all of the radios that are currently accessible. The script then applies a filter to the list of radios to locate the Bluetooth radio, and then uses the SetStateAsync method to change the status of the Bluetooth radio to the value that was specified in the $BluetoothStatus parameter. In the end, the script awaits the asynchronous task that was returned by the SetStateAsync function so that it can wait for the state to be changed.

Step 4: After saving the Script navigate the PowerShell terminal to the location where you save the script. This script accepts an argument while running as BluetoothStatus. And its value can be either on or off.

Step5: If you want to disable the Bluetooth device, type follows command in PowerShell.

.\bluetooth.ps1 -BluetoothStatus On

Here bluetooth.ps1 is the name of the script that you saved in step 1.

Enable Bluetooth
fig. Enable Bluetooth

Step6: If you want to disable the Bluetooth then replace the on with off on the above command.

.\bluetooth.ps1 -BluetoothStatus Off
Disable Bluetooth Using Powershell Script
fig. Disable Bluetooth

Control Bluetooth adapter using Command Prompt

You can use Command Prompt and Batch file to enable and disable bluetooth in windows using following script.

Enable Bluetooth Script

Open notepad and paste down the following script and save it as enablebluetooth.bat .

@echo off echo Turning on Bluetooth... reg add HKLM\SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Keys /v "DisableEncryption" /t REG_DWORD /d 0 /f net start bthserv

now just run enablebluetooth.bat file by double clicking it and you will see bluetooth is enabled.

Disable Bluetooth Script

Open notepad and paste down the following script and save it as disablebluetooth.bat .

@echo off echo Turning off Bluetooth... reg add HKLM\SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Keys /v "DisableEncryption" /t REG_DWORD /d 1 /f net stop bthserv

now just run disablebluetooth.bat file and you will see bluetooth is disabled.

Conclusion

In conclusion, turning on and off Bluetooth adapter on Windows using PowerShell and cmd  script is easy and straightforward. The above script can be used to automate the process of enabling or disabling your Bluetooth adapter.