Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active March 24, 2024 07:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primaryobjects/ce8c7173ff9c6a453cda336aa2e3ff5c to your computer and use it in GitHub Desktop.
Save primaryobjects/ce8c7173ff9c6a453cda336aa2e3ff5c to your computer and use it in GitHub Desktop.
Script to Enable Windows 10 Wifi Mobile Hotspot Automatically and Keep Alive on Disconnect

Enable Windows 10 Mobile Hotspot Automatically and Keep Alive Script

❤️ Sponsor This Project

The following script enables the Windows 10 Wifi Mobile Hotspot and keeps it enabled and active.

The included PowerShell script and associated batch file can be executed once to continuously monitor and keep the Windows 10 Wifi Mobile Hotspot turned on.

Quick Start

  1. Copy the two script files to a folder on your computer: hotspot-keepalive.ps1 and hotspot-keepalive.bat
  2. Edit the path in hotspot-keepalive.bat to point to the directory where you saved the two files.
  3. Run the file hotspot-keepalive.bat.

Additional Notes

PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
PowerShell C:\Users\YOUR_USERNAME\Desktop\hotspot-keepalive.ps1 >> "%TEMP%\StartupLog.txt" 2>&1
# https://superuser.com/a/1434648
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])
}
}
}
# Define a function to check the status of the hotspot
Function Check_HotspotStatus() {
$tetheringManager = Get_TetheringManager
return $tetheringManager.TetheringOperationalState -eq "Off"
}
# Define a function to start the hotspot
Function Start_Hotspot() {
$tetheringManager = Get_TetheringManager
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
$currentDateTime = Get-Date -Format "MM-dd-yyyy HH:mm:ss"
"$currentDateTime Starting hotspot keep-alive."
# Keep alive wifi.
while ($true) {
# Get the current date and time in a specific format
$currentDateTime = Get-Date -Format "MM-dd-yyyy HH:mm:ss"
if (Check_HotspotStatus) {
"$currentDateTime Hotspot is off! Turning it on"
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