:::: MENU ::::

Turn off Bluetooth if no Device is connected actively

Get the Exe file from the following website -

https://www.nirsoft.net/utils/bluetooth_viewer.html

create the following power shell script

$LogFile = "C:\Windows\MSSCCM\Logs\Toggle_Bluetooth_OFF.log"
Function LogWrite
{
Param ([string]$logstring)
$logcontent = "$date $logstring"
Add-Content $Logfile -Value $logcontent
}

New-Item -ItemType Directory -Path "C:\Windows\MSSCCM\Scripts\Bluetooth" -Force -ErrorAction SilentlyContinue

$date=Get-Date -Format g
$PREDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
$InstDir = Split-Path -parent $MyInvocation.MyCommand.Definition

LogWrite "START $date*********************************************"

$Parameters = '/sxml "C:\Windows\MSSCCM\Scripts\bluetooth\bluetooth.xml"'
$Commandtorun = "btView.exe"
$process= Start-Process $CommandToRun $Parameters -PassThru -wait -workingdirectory $InstDir
$ExtVal = $process.ExitCode
logwrite "created the XML file with code:$ExtVal"

if(Test-Path "C:\Windows\MSSCCM\Scripts\bluetooth\bluetooth.xml")
{

logwrite "Searching for any connected devices"

$IS_device_connected = Select-String -Path "C:\Windows\MSSCCM\Scripts\bluetooth\bluetooth.xml" -Pattern "<connected>Yes</connected>" -SimpleMatch -Quiet


if(!$IS_device_connected)
{

logwrite "NO Devices are connected, so toggling the bluetooth setting OFF"

################################################################
#*********TURN OFF BLUETOOTH AS NO DEVICE IS CONNECTED*********#
################################################################
 Function Bluetooth {
 [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 -Verbose -BluetoothStatus Off


LogWrite "Turned OFF the bluetooth on the machine"

####################################################################
####################################################################

}
else
{

LogWrite "Exiting the Script as there are Bluetooth devices connected to the machine "

}


}
else
{

LogWrite "There was an error running the tool to create the XML file"

}

LogWrite "END $date******************************************************"
LogWrite ""


So, what do you think ?