Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active December 25, 2024 15:15
Show Gist options
  • Save primaryobjects/7fef4f8567d9d1f2840d79447041f2cb to your computer and use it in GitHub Desktop.
Save primaryobjects/7fef4f8567d9d1f2840d79447041f2cb to your computer and use it in GitHub Desktop.
Enable Windows 10 Mobile Hotspot with Toggle Wi-Fi Adapter

Enable the Windows 10 Mobile Hotspot Automatically by Toggle Wi-Fi Adapter

❤️ Sponsor This Project

Automatically enable the Windows 10 mobile hotspot by toggling off/on the Windows Wi-Fi adapter and then re-enabling the Mobile Hotspot.

This script is useful for cases where your mobile hotspot frequenty disconnects and requires you to manually re-enable the hotspot by disabling/re-enabling the Wi-Fi adapter and then re-enabling the Mobile hotspot.

Quick Start

  • Save the two files below to your desktop.
  • Right-click on hotspot-keepalive.bat and select Run as administrator.

Additional

See also Script to Enable Windows 10 Mobile Hotspot After Reboot.

PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
PowerShell C:\Users\YOUR_USERNAME\Desktop\hotspot-keepalive.ps1 >> "%TEMP%\StartupLog.txt" 2>&1
# Load necessary assembly
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
}
Function AwaitAction($WinRtAction) {
$asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
$netTask = $asTask.Invoke($null, @($WinRtAction))
$netTask.Wait(-1) | Out-Null
}
Function Get_TetheringManager() {
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
return $tetheringManager;
}
Function SetHotspot($Enable) {
$tetheringManager = Get_TetheringManager
if ($Enable -eq 1) {
if ($tetheringManager.TetheringOperationalState -eq 1) {
"Hotspot is already On!"
} else {
"Hotspot is off! Turning it on"
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
} else {
if ($tetheringManager.TetheringOperationalState -eq 0) {
"Hotspot is already Off!"
} else {
"Hotspot is on! Turning it off"
Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
}
}
Function Check_HotspotStatus() {
$tetheringManager = Get_TetheringManager
return $tetheringManager.TetheringOperationalState -eq "Off"
}
Function Start_Hotspot() {
$tetheringManager = Get_TetheringManager
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
Function Toggle_Wifi() {
"Disabling Wi-Fi adapter."
netsh interface set interface name="Wi-Fi" admin=disable
Start-Sleep -Seconds 5
"Enabling Wi-Fi adapter."
netsh interface set interface name="Wi-Fi" admin=enable
}
$currentDateTime = Get-Date -Format "MM-dd-yyyy HH:mm:ss"
"$currentDateTime Starting hotspot keep-alive."
while ($true) {
$currentDateTime = Get-Date -Format "MM-dd-yyyy HH:mm:ss"
if (Check_HotspotStatus) {
"$currentDateTime Hotspot is off! Turning it on"
Toggle_Wifi
Start-Sleep -Seconds 10 # Wait for the internet connection to establish
"Enabling mobile hotspot."
Start_Hotspot
}
Start-Sleep -Seconds 10 # Wait for 10 seconds before checking again
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment